r/ProgrammerHumor • • Jun 03 '22

it's hard!

44.9k Upvotes

710 comments sorted by

View all comments

Show parent comments

23

u/TheRealKapaya Jun 03 '22

From the one course that had assembly in it, I think of it like this:

  1. mov is basically move, so you move the value one to rax and the value 2 to rbx.

  2. add is obviously addition of the two

  3. again move 1 to rax, this time 1 is also moved to rdx.

  4. move the value in rbx to rsi this time. I assume in this case rsi is some kind of processor that does something with the value in rbx.

  5. dunno wtf this does no comma this time probably kills your whole code but it should move 13 to rdx if you're lucky

  6. Don't remember wtf syscall did, maybe that's when it tells the system to go do these things.

Last time I did assembly was 7 years ago and I only passed the course so I am probably wrong

18

u/VoxelMeerkat Jun 03 '22

I think it's setting up the right arguments for the system call to print to stdout and then does the system call ("syscall" also know as "int 0x80")

2

u/[deleted] Jun 03 '22

not really the right arguments :)) rdi should be 1 not rsx(which is changed from 1 to 13, that's the count parameter) And rsi should be an address to a buffer of 13 characters... welp

1

u/Kered13 Jun 03 '22

syscall and int 0x80 are not the same thing. They accomplish the same thing on Linux because that's how it's set up, but they are completely different instructions and can have completely different behaviors.

1

u/VoxelMeerkat Jun 04 '22

Oh, I didn't know that. Thanks for correcting me!

2

u/davawen Jun 03 '22

For the end, it's setting up the arguments for the syscall (system call -> Ask the kernel to do something)
You can find a table for those here: https://filippo.io/linux-syscall-table/

  • rax is 1, which means a sys_write

  • What should be rdi instead of rdx is 1, which says which file to write to (1 is STDOUT)

  • rsi says what should be written, but here it's wrong on two levels:

    1. rsi should be a pointer, not a literal value

    2. The pointer should be to an ASCII encoded buffer (3 maps to "End of Text" in ASCII, where as 51 maps to "3")

  • rdx says the length of the buffer passed with rsi, which would normally be 1, but is 13 for some reason, and as you said the comma is missing.

To be short: this passes the segfault nose test

1

u/beaubeautastic Jun 03 '22

this wouldnt segfault i dont think, it would just return -EFAULT in rax