问题描述
我试图通过将字符串推入堆栈将字符串传递给过程。我知道命令行参数也被类似地压入堆栈,因此,为了理解它们,我想出了一个人为设计的原型,该原型将ASCI字符的连续数组压入堆栈,并通过一个调用过程访问字符串。但是,在运行代码时,我只能通过printf
格式字符串%c
访问单个字符,而不是数组。使用格式字符串说明符%s
时,出现分段错误。也许我误解了内存寻址的工作原理以及如何正确访问esp
指向的数据。这是我的尝试:
; assemble and link with:
; nasm -f elf test.asm && gcc -m32 -o test test.o
section .text
global main
extern printf
some_proc:
push ebp
mov ebp,esp
mov eax,[ebp+8]
push eax
push format_str
call printf
mov esp,ebp ; Deallocate local variables
pop ebp ; Restore the caller’s base pointer value
ret 4
main:
push 0x00796568 ; hey\0
call some_proc
mov eax,1
int 0x80
section .data
format_str db `%s\n\0` ; <- doesn't work
;format_str db `%c\n\0` <- works
此代码仅输出第一个'h'字符,而不输出字符串hey
的整个连续数组。我需要对此代码进行哪些修改才能执行正确的逻辑/输出?
预期的标准输出:
hey
实际标准输出:
Segmentation Fault
如果您发现我的代码中有任何错误,请通知我,我是汇编语言的新手。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)