同一内存地址处的两个不同值 - 汇编
Two different values at the same memory address - assembly
我在汇编中有一些行为有点奇怪的代码。我有一个 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 的第一个参数,它不会改变功能。我在这里做错了什么?
我犯了一个可以理解的错误。做的时候:
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
我在汇编中有一些行为有点奇怪的代码。我有一个 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 的第一个参数,它不会改变功能。我在这里做错了什么?
我犯了一个可以理解的错误。做的时候:
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