r/dailyprogrammer • 1 2 • Nov 08 '13

[11/4/13] Challenge #140 [Easy] Variable Notation

(Easy): Variable Notation

When writing code, it can be helpful to have a standard (Identifier naming convention) that describes how to define all your variables and object names. This is to keep code easy to read and maintain. Sometimes the standard can help describe the type (such as in Hungarian notation) or make the variables visually easy to read (CamcelCase notation or snake_case).

Your goal is to implement a program that takes an english-language series of words and converts them to a specific variable notation format. Your code must support CamcelCase, snake_case, and capitalized snake_case.

Formal Inputs & Outputs

Input Description

On standard console input, you will be given an integer one the first line of input, which describes the notation you want to convert to. If this integer is zero ('0'), then use CamcelCase. If it is one ('1'), use snake_case. If it is two ('2'), use capitalized snake_case. The line after this will be a space-delimited series of words, which will only be lower-case alpha-numeric characters (letters and digits).

Output Description

Simply print the given string in the appropriate notation.

Sample Inputs & Outputs

Sample Input

0
hello world

1
user id

2
map controller delegate manager

Sample Output

0
helloWorld

1
user_id

2
MAP_CONTROLLER_DELEGATE_MANAGER

Difficulty++

For an extra challenge, try to convert from one notation to another. Expect the first line to be two integers, the first one being the notation already used, and the second integer being the one you are to convert to. An example of this is:

Input:

1 0
user_id

Output:

userId
59 Upvotes

136 comments sorted by

View all comments

27

u/m42a Nov 09 '13

Linux-specific x64 assembly

.globl _start

.set READ_NUM, 0
.set WRITE_NUM, 1
.set EXIT_NUM, 60

.set STDIN, 0
.set STDOUT, 1

.set SPACE, 32
.set ZERO, 48
.set ONE, 49
.set TWO, 50
.set UNDERSCORE, 95
.set LOWERCASE_A, 97
.set LOWERCASE_Z, 122


exit_0:
	movq $EXIT_NUM, %rax
	movq $0, %rdi
	syscall

exit_1:
	movq $EXIT_NUM, %rax
	movq $1, %rdi
	syscall

//Returns read's status in rax (should always be 1) and the byte read in rbx
read_byte:
	movq $READ_NUM, %rax
	movq $STDIN, %rdi
	leaq -8(%rsp), %rsi
	movq $1, %rdx
	syscall
	//Exit if we run out of input
	cmpq $0, %rax
	je exit_0
	//Exit with failure if there's an error
	cmpq $0, %rax
	jl exit_1
	movq -8(%rsp), %rbx
	ret

//Writes the byte in rbx and returns write's status in rax (should always be 1)
write_byte:
	movq %rbx, -8(%rsp)
	movq $WRITE_NUM, %rax
	movq $STDOUT, %rdi
	leaq -8(%rsp), %rsi
	movq $1, %rdx
	syscall
	//Exit with failure if there's an error
	cmpq $0, %rax
	jl exit_1
	ret

//Make rbx uppercase if it was lowercase
to_upper:
	leaq -32(%rbx), %r13
	cmp $LOWERCASE_A, %rbx
	cmovb %rbx, %r13
	cmp $LOWERCASE_Z, %rbx
	cmova %rbx, %r13
	mov %r13, %rbx
	ret


camel_case:
	//Skip newline after selection
	call read_byte
	//The previous character wasn't a space
	movq $0, %r12
camel_loop:
	call read_byte
	//If we don't have a space
	cmp $SPACE, %rbx
	jne not_space
	//Don't output the space, just set a flag
	movq $1, %r12
	jmp camel_loop
not_space:
	cmp $1, %r12
	jne camel_write
	//We might need to capitalize
	call to_upper
	movq $0, %r12
camel_write:
	call write_byte
	jmp camel_loop


snake_case:
	//Skip newline after selection
	call read_byte
snake_loop:
	call read_byte
	//Change space to underscore
	mov $UNDERSCORE, %r13
	cmp $SPACE, %rbx
	cmove %r13, %rbx
	call write_byte
	jmp snake_loop


upper_case:
	//Skip newline after selection
	call read_byte
upper_loop:
	call read_byte
	//Change space to underscore
	mov $UNDERSCORE, %r13
	cmp $SPACE, %rbx
	cmove %r13, %rbx
	call to_upper
	call write_byte
	jmp upper_loop

_start:
	call read_byte
	cmp $ZERO, %rbx
	je camel_case
	cmp $ONE, %rbx
	je snake_case
	cmp $TWO, %rbx
	je upper_case
	jmp exit_1

6

u/logicalunit Nov 09 '13

I can't see anything after I read that.

4

u/pirate_platypus Nov 09 '13

Wow... I've never seen someone use assembly for a programming challenge. Considering I've never done anything with assembly (not even reading), I'm surprised how readable it is.

1

u/th00ht May 04 '22

I find your lack of register use disturbing.