Return difference between higher and lower array value in Assembly

Asked

Viewed 692 times

1

I am trying to make a code in Assembly with Arq. x86 using NASM.

In this code I must return the difference between the highest value and the lowest value of an array, as below.

However, when running it is returning 14 to the example below and the correct one would be 18 (20-2). Could you help me find my error? Most grateful at once.

section .data
    array: DB 2,4,6,8,10,12,14,16,18,20
    size EQU $-array

section .text

global _start
_start:

    mov eax,[array]
    mov ebx,[array]

    xor ecx,ecx
    mov edx,size

jmp compara

maior:
    mov eax,[array+ecx]
    jmp volta_maior

menor:
    mov ebx,[array+ecx]
    jmp volta_menor

compara:
    cmp ecx,edx
    jnle fim
    cmp [array+ecx],eax
    jge maior
volta_maior:
    cmp [array+ecx],ebx
    jle menor
volta_menor:
    inc ecx
    jmp compara

fim:

    sub eax,ebx
    mov ebx,eax
    mov eax,1
    int 0x80

1 answer

2


There are 2 problems with the program:

1) How the array is formed by bytes, all data operations (load, comparisons, etc.) must be done with 8-bit registers and not 32. Example:

_start:

    mov al,[array]      ; carrega o registrador al (8 bits) com o valor [array]
    mov bl,[array]      ; carrega o registrador bl (8 bits) com o valor [array]
    ...

2) The end of looping should occur when ecx is equal to (or greater than) edx:

compara:
    cmp ecx,edx
    jge fim                 ; Executa até que ecx >= edx
    cmp [array+ecx],eax
    jge maior

The modified program (with looping fixed and changed registers for the 8 bits) is like this:

section .data
    array: DB 2,4,6,8,10,12,14,16,18,20
    size EQU $-array

section .text

global _start
_start:

    mov al,[array]
    mov bl,[array]

    xor ecx,ecx
    mov edx,size

    jmp compara

maior:
    mov al,[array+ecx]
    jmp volta_maior

menor:
    mov bl,[array+ecx]
    jmp volta_menor

compara:
    cmp ecx,edx
    jge fim
    cmp [array+ecx],al
    jge maior

volta_maior:
    cmp [array+ecx],bl
    jle menor

volta_menor:
    inc ecx
    jmp compara

fim:
    sub al,bl
    movzx ebx,al
    mov eax,1
    int 0x80

After execution:

~/$ ./a.out
~/$ echo $?
18
~/$
  • 1

    I was doing online for not having the nasm or other Assembler, and no getting, I think the problem is the online that has problem with IO.

  • 2

    Yes, the program does not have IO and the online compiler needs to display the result through the return code for the S.O. No Jdoodle it is possible to compile and view this return code :)

  • 1

    I didn’t know that one, cool...

  • P.S.: I use https://tio.run/#Assembly-nasm to compile. It is very interesting and also allows to see the return!

  • @Gomiero in your code in the "compare" function he does "Cmp [array+ecx],al" and if it is greater or equal he goes to "greater" and otherwise he goes on and does "Cmp [array+ecx],Bl", is that it? Thanks again!

  • @Léoeduardosilva Yes, it’s the same question code. I just switched the registers eax and ebx for al and bl.

  • Got it. Thank you, @Gomiero. Thank you also for the explanation!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.