asm
asm copied to clipboard
Clarification on mov of a pointer address as counter
Sup guys! I'm still learning from your tutorials, but this one instruction left me confused Chapter 4:Example
;; Print the reversed string to the standard output.
printResult:
;; Set the length of the result string to print.
mov rdx, rdi
Here we mov rdx, rdi, but rdi was storing the value of the OUTPUT buffer, and for my understanding, in the previous loop it was moving forward the pointer. When we call the syscall write() won't rdx be a enormous pointer address instead of the length of the string?
I don't know why, but the code works :P
Can you guys make this section more clear so we can understand why it works, maybe using the paragraph before the example?
@edu-bm7 Thanks for the issue. I will recheck and try to improve it during weekends
@edu-bm7 thank you one more time, it was really good catch! It worked "by accident". You are right that the rdi contains the address instead of the length of the string. But as the OUTPUT is defined in the .bss section it is filled with zeros. You may see it in debugger:
(gdb) info address OUTPUT
Symbol "OUTPUT" is at 0x401010 in a file compiled without debugging.
(gdb) x/15xb 0x401010
0x401010 <OUTPUT>: 0x21 0x64 0x6c 0x72 0x6f 0x77 0x20 0x6f
0x401018: 0x6c 0x6c 0x65 0x48 0x00 0x00 0x00
(gdb) x/100xb 0x401010
0x401010 <OUTPUT>: 0x21 0x64 0x6c 0x72 0x6f 0x77 0x20 0x6f
0x401018: 0x6c 0x6c 0x65 0x48 0x00 0x00 0x00 0x00
0x401020: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401028: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401030: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401038: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401040: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401048: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401050: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401058: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401060: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401068: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x401070: 0x00 0x00 0x00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., 4198428!dlrow olleH) = 2048
write(1, "\n", 1
) = 1
+++ exited with 0 +++
As null character is non printable we see the only reversed string :) I've fixed the example in the https://github.com/0xAX/asm/pull/51, now it should be correct.
@0xAX Thank you for the explanation, I was really confused for why it worked, thinking I was missing something! 😄 I want to thank you for creating and maintaining this tutorial, I was lost trying to learn assembly, now I'm a little more confident with it because of this guide!
#51 is merged, so I am closing the issue. @edu-bm7 feel free to re-open if something is still unclear.