【发布时间】:2015-06-25 09:59:57
【问题描述】:
我是组装新手。对于遵循我所期望的简单代码,我有不同的输出。每次调用printf 之前,eax 的内容都会向右移动某个数字。我究竟做错了什么?谢谢。
代码:
;file name : testing.asm
;assemble and link with:
;nasm -f elf testing.asm && gcc -m32 -o testing testing.o
extern printf ; the C function, to be called
SECTION .data ; Data section, initialized variables
a: dd 15 ; int a=15
str: db "content in eax=%d", 10, 0
SECTION .text ; Code section.
global main ; the standard gcc entry point
main: ; the program label for the entry point
mov ebp, esp
mov eax, [a] ; put a from store into register
shr eax, 1 ; eax content should be 15>>1 = 7
push eax
push dword str ; string to be printed
call printf ; printf(str,content_of_eax)
shr eax, 2
push eax ; eax content should be 7>>2 = 1
push dword str
call printf ; printf(str,content_of_eax)
shr eax, 1
push eax ; eax content should be 1>>1 = 0
push dword str
call printf ; printf(str,content_of_eax)
mov esp, ebp ; takedown stack frame
mov eax, 0 ; normal, no error, return value
ret ; return
输出:
content in eax=7
content in eax=4
content in eax=8
预期输出:
content in eax=7
content in eax=1
content in eax=0
【问题讨论】:
-
您的前四个字:“... 我是汇编新手 ...” 对于刚接触汇编的人来说,您确实编写了不错的源代码;整洁,乍一看很容易阅读,完全清晰易读,对于以前从未见过您或您的作品的陌生人来说是可以理解的。继续这样做,你会有一个光明的未来。
-
感谢您的赞赏 :)
标签: c linux assembly printf nasm