0

I'm trying to write a function that takes a code of symbol and prints it to stdout. I wrote this code using examples of functions that prints string

print_char:
    push rdi                  
    mov rsi, rsp
    mov rdx, 1
    mov rdi, 1
    mov rax, 1
    syscall
    pop rdi
    ret

And I don't understand how we use rdx here - in print string function example it contains length of the string. I tried to find the answer in the intel manual and forums, but I still don't understand how program uses rdx to print symbol? Thanks

  • `rdx` is the length in bytes, the 3rd arg to your `write(1, &stack_location_holding_arg, 1)` system call. Of course you won't find anything about it in Intel's manuals; the CPU doesn't have an OS or text print functions built in, or any notion of what stdout is. `syscall` is calling into the kernel; Linux in this case judging by the call number and ABI. See [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) for an explanation of making Linux system calls in a 32-bit hello world program. – Peter Cordes Nov 23 '21 at 08:56
  • Ah, it seems there's a Q&A [Writing a putchar in Assembly for x86\_64 with 64 bit Linux?](https://stackoverflow.com/q/50681077) a couple years ago, about doing a 1-byte `write` like this code is. Although I used a `mov` store instead of `push` to get the byte into memory where I could pass `write` a pointer to it. Anyway, it's the same system call as in the print-string case, that's why RDX works the same. – Peter Cordes Nov 23 '21 at 09:04

0 Answers0