1

I'm currently working my way through learning writing my own programming language. Currently my language supports adding two numbers or subtracting and I want to now convert my AST into assembly. I'm running on a Linux machine in the x86_64 architecture and I've gone through the hello world tutorial with code that looks like:

section .data                                                                                                                                    
    string1 db  "Hello World!",10,0                                             
                                                                                 
section .text                                                                   
    global _start                                                               
    _start:                                                                     
        mov rax, 1                                                              
        mov rdi, 1                                                              
        mov rsi, string1                                                        
        mov rdx, 13                                                             
        syscall                                                                 
        mov rax, 60                                                             
        mov rdi, 0                                                              
        syscall

and I get what is happening here. Where I'm a little lost is in code like so:

section .text                                                                   
    global _start                                                               
    _start:
        mov rax, 1
        mov rbx, 2
        add rax, rbx
        mov rsi, rbx                                                                     
        mov rax, 1                                                              
        mov rdi, 1                                                        
        mov rdx, 1                                                             
        syscall                                                                 
        mov rax, 60                                                             
        mov rdi, 0                                                              
        syscall

As I understand what I've been reading, the above code should put the value 1 into the register rax, 2 into the register rbx, sum those registers and store it in rbx. Then, the value of rbx should be moved into rsi which should be the buffer argument for the syscall for write. Instead, nothing is printed to my console. If I put "3" in the .data section as a variable I can grab that and print it. So is it just the case that I cannot print an integer and it has to be stored in some data variable?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Michael Platt
  • 1,297
  • 12
  • 25
  • Is `write(1, (char*)3, 1)` a valid *pointer* to a buffer of data? (https://man7.org/linux/man-pages/man2/write.2.html). No, it isn't. `strace ./a.out` would show you that the system call returns `-EFAULT`. It has to be in memory; it doesn't have to be in static storage. – Peter Cordes Sep 03 '22 at 13:03

1 Answers1

1

Without knowing the exact syntax of the syscalls, it seems to me that you would at least need to convert the result from the addition (3) into a string ("3", which is 51 is ASCII -- try adding 48 for single-digit numbers). A byte value of 3 does not encode a printable character, so the output will likely not show up in the console even if your call to the "print" routine is correct.

fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
  • `write(1, (char*)3, 1)` is also not a valid pointer, so it's actually returning `-EFAULT`, not writing any bytes to stdout. – Peter Cordes Sep 03 '22 at 13:05