【发布时间】:2018-03-26 17:39:41
【问题描述】:
在 linux 中,您可以使用系统调用号 4 打印一些内容:
mov eax,4 ;system call number
mov ebx,0 ;file descriptor
mov ecx,msg ;adress of message in data segment
mov edx,length ;length of message
但是,你如何从堆栈段打印一些东西?
我试过了:
push 'H'
push 'e'
push 'l'
push 'l'
push 'o'
push ' '
push 'w'
push 'o'
push 'r'
push 'l'
push 'd'
mov eax,4 ;system call number
mov ebx,0 ;file descriptor
mov ecx,ebp ;adress of message
mov edx,11 ;length of message
但不打印任何内容。
编辑:我对我的代码做了一些更改,现在是这样:
section .data
msg: db "Hola mundo",0Ah
ok: db "OK",0Ah
section .text
global _start
_start:
push 'leH'
push 'w ol'
push 'dlro'
mov eax,4 ;system call number
mov ebx,1 ;file descriptor
mov ecx,esp ;adress of message in data segment
mov edx,11 ;length of message
mov eax,1
xor ebx,ebx ;same as move ebx,0 but better
int 0x80
EDIT 2(仍然不工作)
section .data
msg: db "Hello world",0Ah
section .text
global _start
_start:
push 'leH'
push 'w ol'
push 'dlro'
mov eax,4 ;system call number
mov ebx,1 ;file descriptor
mov ecx,esp ;adress of message in data segment
mov edx,11 ;length of message
int 0x80
mov eax,1
xor ebx,ebx ;same as move ebx,0 but better
int 0x80
响应评论,我组装和编译:
nasm -f elf64 hello.asm && ld hello.o && ./a.out
我正在使用 Ubuntu 64 位 Linux。
【问题讨论】:
-
ebp不是堆栈指针,esp是。此外,push将使用每个字符 4 个字节,这样就不太行了。 -
更不用说使用
push会反转你的字符串。 -
@Jester 我想测试字符串的反转
-
正如 Jester 指出的应该是 ESP 和 EBP 并且如果您想向后打印该字符串,则可以将 Pushes 替换为
push 'leH'push 'w ol'push 'dlro'。对于标准输出,EBX 也应该是 1 而不是 0。 (0 是标准输入) -
您是将其组装成 64 位程序还是 32 位程序?此代码无法作为 64 位代码正常运行,因为 64 位代码中的
int 0x80仿真无法处理使用堆栈时所需的 64 位地址。堆栈地址需要 RSP 中的完整 64 位地址。只有通过syscall指令使用64 位System V Linux 系统调用接口才能做到这一点。可以在Ryan Chapman's blog 中找到有关使用的信息。
标签: assembly memory nasm x86-64 system-calls