同一内存地址的两个不同值 - 汇编

时间:2016-11-26 16:52:17

标签: c assembly nasm

我在汇编中有一些代码有点奇怪。我有一个C extern函数,它使用.asm文件中的 asm 调用另一个函数。这个C函数将我的函数从.asm文件中使用的三个地址放在堆栈上。一切顺利,直到出现:

; Let's say we take from the stack first parameter from my C function.
; This parameter is a string of bytes that respect this format:
;   - first 4 bytes are the sign representation of a big number
;   - second 4 bytes are the length representation of a big number
;   - following bytes are the actual big number

section .data
  operand1 dd 0

section .text
global main

main:
  push ebp
  mov ebp, esp
  mov eax, [ebp + 8] ; Here eax will contain the address where my big number begins.
  lea eax, [eax + 8] ; Here eax will contain the address where 
                     ; my actual big number begins.   
  mov [operand1], eax

  PRINT_STRING "[eax] is: "
  PRINT_HEX 1, [eax] ; a SASM macro which prints a byte as HEX
  NEWLINE

  PRINT_STRING "[operand1] is: "
  PRINT_HEX 1, [operand1]
  NEWLINE 

  leave
  ret 

运行此代码时,我在终端输入正确的[eax]输出,而对于[operand1],它会保持打印一个数字,如果我修改了我的C函数的第一个参数,这个数字不会改变。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我犯了一个可以理解的错误。在做的时候:

mov [operand1], eax
PRINT_STRING "[operand1] is: "
PRINT_HEX 1, [operand1]
NEWLINE

此代码打印包含在此局部变量(operand1)所在地址的内容的第一个字节(,即我的实际大数字开始的地址)。为了获得驻留在[operand1]的实际值,我必须这样做:

mov ebx, [operand1]
PRINT_STRING "[operand1] is: "
PRINT_HEX 1, [ebx]
NEWLINE
相关问题