Why cant i sys_write from a register? [duplicate]

2019-01-28 17:42发布

This question already has an answer here:

; NASM
push 30 ; '0'

mov rax, 4 ; write
mov rbx, 1 ; stdout
mov rcx, rsp ; ptr to character on stack
mov rdx, 1 ; length of string = 1
int 80h

The code above does not print anything to stdout. It works when i give it a ptr to a character in section .data. What am i doing wrong?

2条回答
甜甜的少女心
2楼-- · 2019-01-28 18:02

30 decimal is the code of the ASCII "record separator". Whatever that is, it's probably not a printable character.

30 hexadecimal (30h or 0x30 in NASM parlance), on the other hand, is the code of the ASCII "0".

Also, you need to use the 64-bit ABI.

查看更多
乱世女痞
3楼-- · 2019-01-28 18:20

amd64 uses a different method for system calls than int 0x80, although that might still work with 32-bit libraries installed, etc. Whereas on x86 one would do:

mov eax, SYSCALL_NUMBER
mov ebx, param1
mov ecx, param2
mov edx, param3
int 0x80

on amd64 one would instead do this:

mov rax, SYSCALL_NUMBER_64 ; different from the x86 equivalent, usually
mov rdi, param1
mov rsi, param2
mov rdx, param3
syscall

For what you want to do, consider the following example:

        bits 64
        global _start

section .text

_start:
        push            0x0a424242
        mov             rdx, 04h
        lea             rsi, [rsp]
        call            write
        call            exit
exit:
        mov             rax, 60     ; exit()
        xor             rdi, rdi    ; errno
        syscall

write:
        mov             rax, 1      ; write()
        mov             rdi, 1      ; stdout
        syscall
        ret
查看更多
登录 后发表回答