Disclaimer: I am a beginner, and this is the worst language i have ever seen
The goal is to decode a message.
Each character is encoded in 8 bytes
- 7-8 byte doesnt matter, assume 0
- 3-6 byte index
- 2nd byte amount (times the character needs to be printed)
- 1st byte character
The character and amount are correct, but for some reason I can’t get the right index.
With the index I can go to the next address of the character that needs to print from $message
This isn’t just some segmentation fault, so i don’t really understand why i don’t get the correct index value. Right now i get:
“HWo
l!olred” instead of “Hello world!”
decode:
pushq %rbp
movq %rsp, %rbp
pushq %rdi
pushq $4 # 16 Stack allignment
movq (%rdi), %rax # Load 8-byte message from address in %rdi
movb %al, %sil # Move the least significant byte of %rax to %sil
shr $8, %rax # Shift %rax right by 8 bits
movb %al, %bl # Move the least significant byte of %rax to %bl
shr $8, %rax # Shift %rax right by 8 bits
.loop:
cmpl $0, %eax
je .finish
movq $String, %rdi # Move the format string to %rdi
call printf # Print the character
dec %bl # Decrement the "amount" counter
cmpb $1, %bl # Compare %bl (amount) with 1
jg .loop
popq %rdi
popq %rdi # Remove 16 bytes
imull $8, %eax # Multiply the index by 8 (8 byte memory blocks)
addq %rax, %rdi
call decode
.finish:
movq %rbp, %rsp
popq %rbp
ret
main:
pushq %rbp # Push the base pointer (and align the stack)
movq %rsp, %rbp # Copy stack pointer value to base pointer
movq $MESSAGE, %rdi # First parameter: address of the message
call decode # Call decode
popq %rbp # Restore base pointer location
movq $0, %rdi # Load program exit code
call exit # Exit the program
2