请问这个两数相加的装配工作 [英] How does this adding of two numbers works in Assembly

查看:118
本文介绍了请问这个两数相加的装配工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的汇编语言,并试图了解一个简单的程序,这将增加两个nunbers并显示结果。

I am new to assembly language and trying to understand a simple program which will add two nunbers and display the result.

section .data

message1 db "value=%d%d",10,0

 section .text
   global main
   extern printf
   main:
   mov eax, 55
   mov ebx, 45
   add eax,ebx
   push eax
   push ebx
   push message1
   call printf
   add esp, 8
   ret

现在输出出来是45 100

Now output comes out is 45 100

添加EAX EBX指令后,结果将被储存在EAX寄存器。

After add eax ebx instruction result will be stored in eax register.

但是,现在,未来线发生什么

But now what happen in coming lines

 push eax   // push 100 on to stack

 push ebx   // push 45 on to stack

 push message1  // push "value=%d" on to stack // I m bit doubtful here 

我想知道的是在执行调用printf - 发生什么

What I would like to know is what happen when "call printf" is executed??

什么是ADD ESP,8的puspose?

What is the puspose of "add esp,8"??

推荐答案

的printf librarymay多种方式来实现的,所以这将是危险的断言所有 的printf 程序会以这样的方式执行该 的printf 的作用。

The printf librarymay be implemented in many ways, so it would be dangerous to assert that ALL printf routines will execute in the manner that THIS printf acts.

顺序

推EAX //推100堆栈
   推EBX //上推45叠
   推MESSAGE1 //推送消息值=%d个地址到堆栈
   调用printf //推返回地址栈

push eax // push 100 on to stack push ebx // push 45 on to stack push message1 // push THE ADDRESS OF the message "value=%d" onto stack call printf // push the RETURN ADDRESS to the stack

进入与的printf 常规,从底部读出栈

enters the printf routine with, reading the stack from the BOTTOM


  1. 返回地址

  2. 一个指向消息

  3. 某些参数值

因此​​, PRINTF 最有可能


  1. POP 返回地址并保存

  2. POP 的指针信息

  3. MOV E中的堆栈指针寄存器或保存

  1. POP the return address and save it
  2. POP the pointer to the message
  3. MOVe the STACK POINTER to a register or save it

然后,它可以去它的任务 - 使用指针消息,写每一个字符,直到遇到像%d个 A KeyString中它说'打印的东西作为小数。因此, POP ■从堆栈中的下一个值(45,如推入 EBX ),格式为十进制并打印,然后用继续的printf 字符串。

Then it can go about its task - using the pointer to the message, write each character out until it encounters a keystring like %d which says 'print something as a decimal. So it POPs the next value from the stack (45, as pushed in ebx), formats that as a decimal and prints it, then continues with the printf string.

另一个%d个 - 100从 EAX 推,然后继续 - 直到你找到 0 字节表示结束的字符串。

Another %d - the 100 pushed from eax, then continue - until you find the 0 byte indicating end-of-string.

所有的printf 现在需要做的回归是恢复堆栈指针不管它被存储起来,返回到返回地址 - 这就是被存储的地方。

All printf needs to do now to return is to restore the stack pointer from wherever it was stored, and return to the return address - wherever that's been stored.

和当它返回时,堆栈恢复到它到底是什么,当的printf 被称为 - 在那个时候, EBX EAX 曾经 PUSH 编辑。每个为4个字节,因此堆栈指针需要由8个字节,以消除这两个 PUSH 的说明。

And when it returns, the stack is restored to exactly what it was when the printf was called - and at that time, EBX and EAX had been PUSHed. Each is 4 bytes, so the stack pointer needs to be adjusted by 8 bytes to remove the data stored by these two PUSH instructions.

所以 - 为什么这样做的 - 为什么不干脆allw PRINTF 来调整栈 - 它可以,因为它知道它取消了显示8个字节(2 * %D)?

So - why do it that way - why not simply allw PRINTF to adjust the stack - which it could, since it knows it's removed 8 bytes for display (2*%d)?

那么,在本质上,它可以 - 但假设消息仅包含在一个%d个 - 或3 - 或者一些消费的东西比8字节?在返回时,堆栈指针将包含意外的值 - 这取决于如何 PRINTF 间$ P $点一串。很难拉汇编技巧,例如覆盖的消息部分withou是格外小心。因为它是写的,的printf 函数总是表现在predictable方式,返回已经弹出消息的地址,无论任何其他考虑。由程序员妥善处理堆栈内容。

Well, in essence, it could - but suppose the message only contained one %d - or 3 - or something that consumed something OTHER than 8 bytes? On return, the stack-pointer would contain an unexpected value - which depends on how PRINTF interprets a string. Very difficult to pull assembler tricks like overwriting parts of messages withou being extraordinarily careful. As it's written, the printf function always acts in a predictable manner, returning having popped off the message address, regardless of any other consideration. Up to the programmer to properly deal with the stack contents.

这篇关于请问这个两数相加的装配工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆