【问题标题】:Two different values at the same memory address - assembly同一内存地址上的两个不同值 - 汇编
【发布时间】:2016-11-26 16:52:17
【问题描述】:

我在汇编中有一些行为有点奇怪的代码。我有一个 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 函数的第一个参数,该数字不会改变。我在这里做错了什么?

【问题讨论】:

  • 请将您问题的答案作为答案发布,并将其标记为正确。然后其他用户可以看到问题解决了。
  • 使用 *asm 调用 .asm 文件中的另一个函数*:什么?您不需要使用 inline-asm 来调用在.asm 文件中定义的函数。写一个原型,正常调用就行了。

标签: c assembly x86 nasm


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2014-12-17
    • 2021-12-18
    • 2011-06-02
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多