0

I want to write an assembly code that prints "Hello, World!", then reads input from user and then prints that input. Getting input is correct, but i can not print it (Or prints the old value). Code :

.global _start
.intel_syntax noprefix

_start:

    # Write
    mov rax, 1          # syscall number for sys_write
    mov rdi, 1          # file descriptor 1 (stdout)
    lea rsi, load_msg   # pointer to the message to be written
    mov rdx, 14         # message length
    syscall

    # Read
    mov rax, 0          # syscall number for sys_read
    mov rdi, 0          # file descriptor 0 (stdin)
    lea rsi, read_msg   # pointer to the buffer to store user input
    mov rdx, 255        # maximum number of bytes to read
    syscall

    # Write
    mov rax, 1          # syscall number for sys_write
    mov rdi, 1          # file descriptor 1 (stdout)
    mov rdx, rax 
    syscall             # writing the user input to the standard output

    # Exit
    mov rax, 60         # syscall number for sys_exit
    mov rdi, 0          # exit code 0
    syscall

load_msg:
    .asciz "Hello, World!\n"

read_msg:
    .space 256

  • `mov rdx, rax` should probably be before `mov rax, 1` – ecm Jul 25 '23 at 10:18
  • @ecm Yes, but that doesn't change the result – Merd Ceferzade Jul 25 '23 at 10:20
  • 2
    You probably have to switch sections to get a writable buffer for `read_msg` – ecm Jul 25 '23 at 10:52
  • 1
    @ecm: Correct, GAS defaults to the `.text` section, which is read+exec but not write. `strace ./a.out` should show `read` returning `-EFAULT` unless there are other bugs. The OP claims "getting input is correct" but doesn't appear to have checked with a debugger. Also, the LEAs should be RIP-relative, otherwise there's no point to using LEA. `lea rsi, [RIP + read_msg]`. [How to load address of function or label into register](https://stackoverflow.com/q/57212012) . But if that was actually a problem (in a PIE) you'd get a link error. – Peter Cordes Jul 25 '23 at 15:22

0 Answers0