简体   繁体   中英

Assembler: In edi-register stored value changes. Why?

I have written this assembly program:

start:
    call read_hex

    mov edi, eax ; edi stores the limit.
    mov esi, 0 ; esi stores the counting variable.
loopBody:
    inc esi
    mov eax, esi
    call print_eax
    mov eax, edi
    call print_eax
    cmp edi, esi
    JE finishUp

    call read_hex
    add ebx, eax

    JMP loopBody

finishUp:
    mov eax, edi
    call print_eax
    push    0
    call    [ExitProcess]

The belonging assignment is:

Write a program that takes the value m as input. It then receives m consecutive numbers from the user, sums all those numbers and prints back the total sum to the console.

See assignment 0.1: https://github.com/xorpd/asm_prog_ex/blob/master/4_basic_assembly/0_branching/2_basic_conditional_branching/ex/2_write_code/write_code.txt

My idea is to store the limit the user have entered into edi.

esi is used as the counting variable.

Now let's say I have entered 3 as the limit (It shall count three consecutive numbers). 0 is what I enter as input every time when the next number is expected.

I get the following result:

在此处输入图片说明

The first time passing through the loop everything is expected.

esi is 1 and edi is 3.

But next loop passthrough the value of edi has changed to 6ff7b !!

Why has edi changed it's value?

I haven't touched it nowhere!

The problem was caused by the included function "read_hex".

The code of "read_hex" can be seen here:

https://github.com/xorpd/asm_prog_ex/blob/master/include/training.inc

edi is changed multiple times within the function. And by that was caused the unexpected behaviour.

So therefore to all other assembler beginners:

Be careful when including code from someone else. Remember that it can change your program's state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM