调用atoi(汇编程序)printf(汇编程序)

时间:2012-02-12 02:45:16

标签: assembly

我在Assembly中练习编程,编写代码C. 我不明白使用atoi将参数转换为整数。 有人可以向我解释我如何解释以下代码段:

movl    12(%ebp), %eax  ; move a parameter to %eax
addl    $4, %eax        ; add 4 bytes
movl    (%eax), %eax    ; magic things
movl    %eax, (%esp)    ; magic things
call    atoi            ; atoi call
movl    %eax, 24(%esp)  ; asign the result of a magic procedure to a new variable

我理解一些指示,但魔术程序对我来说有点模棱两可。

另外,我想知道函数printf的调用是如何工作的,这是代码的一部分:

movl    $.LC1, %eax  ; assing the string (%d\n) to %eax
movl    28(%esp), %edx  ; move the value to print to %edx
movl    %edx, 4(%esp)   ; magic things
movl    %eax, (%esp)    ; magic things
call    printf          ; call to printf

提前感谢您的支持。

2 个答案:

答案 0 :(得分:1)

%eax是存储在寄存器中的值

(%eax)是使用存储在eax

中的值在内存中的值

E.g。 movl 4, %eax
这会将eax的值设置为4。

(%eax)的值现在是位于内存中4的地址。

movl    (%eax), %eax    ; move the value in memory of eax  (value eax points to) to the address of register eax
movl    %eax, (%esp)    ; move the address of eax to the value in memory of esp (value that esp points to)

movl    %edx, 4(%esp)   ; move the address of edx to the value in memory of esp + 4 
movl    %eax, (%esp)    ; move the address of eax to the value in memory of esp

你的第一个例子只有movl %eax, (%esp)的原因是因为atoi只接受一个参数。

第二个示例使用movl %edx, 4(%esp),因为eax已经被使用,而printf有两个参数。

答案 1 :(得分:0)

括号是寄存器间接寻址(想想指针解除引用)。 number(register)表示“在解除引用之前添加偏移number。”

有关如何调用其他功能,请参阅系统的calling conventions。对于x86-32,请参阅this

相关问题