【发布时间】:2015-04-04 15:41:02
【问题描述】:
我正在尝试通过在汇编代码中调用 printf 来按字符打印字符串,但是在打印第一个字符后出现分段错误,我不明白为什么会发生这种情况,有人可以帮忙吗请问??
section .rodata
lc:
DB "%c", 10, 0
section .text
align 16
global my_func
extern printf
my_func:
push ebp
mov ebp, esp ; Entry code - set up ebp and esp
pusha ; Save registers
mov ecx, dword [ebp+8] ; Get argument (pointer to string)
incr:
cmp byte [ecx], 0
jz end
movzx eax, byte [ecx]
push eax
push lc
call printf
add esp, 8
inc ecx
jmp incr
end:
popa ; Restore registers
mov esp, ebp ; Function exit code
pop ebp
ret
【问题讨论】:
-
eax、ecx和edx是 调用者保存 寄存器。因此,您不应依赖任何这些寄存器的值在函数调用中保留。
标签: assembly x86 segmentation-fault printf