2016-11-26 23 views
0

我在程序集中有一些代碼有點奇怪。我有一個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]正確的輸出,以及[操作數1]它不斷印刷數,如果我修改,這將不會改變我的C函數的第一個參數。我在這裏做錯了什麼?

+1

請將問題的答案作爲答案發布,並將其標記爲正確。然後其他用戶可以看到問題已解決。 – alain

+0

*調用* asm * .asm文件中的另一個函數*:什麼?您不需要使用inline-asm來調用在'.asm'文件中定義的函數。只需編寫一個原型並正常調用。 –

回答

0

我犯了一個可以理解的錯誤。當這樣做的:

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

此代碼打印內容的第一個字節(這是我的實際大數開始地址)包含在其中,這個局部變量(操作數)所在的地址。爲了得到[operand1]中的實際值,我必須這樣做:

mov ebx, [operand1] 
PRINT_STRING "[operand1] is: " 
PRINT_HEX 1, [ebx] 
NEWLINE 
相關問題