Home > Net >  Checking for two-digit numbers in assembly , and then summing them
Checking for two-digit numbers in assembly , and then summing them

Time:10-15

To be fair I'm completely lost and this code is probably completely wrong, but I wanted to ask how do I create a loop that checks for two digit numbers in my array in assembly.enter image description here

CodePudding user response:

As far as I understood, you have an array with numbers in it. And you want to find the sum of 2-digit numbers. To do that, first, let us define an array and a sum variable. Putting a 0xFFFF at the end of our list will help us locate the end of the list.

arr: dw 15, 16, 9, 8, 0xFFFF
sum: dw 0x0000

Now we need to iterate over the array and find two-digit numbers:

start:
    mov ecx, 0 ; keep counter

.loop:
    mov ebx, arr ; get the address of the array
    add ebx, ecx ; get the nth word

    cmp word [ebx], 0xFFFF ; check if we are at the end of list
    je endLoop ; if we are end the loop
    
    add ecx, 2 ; add 2 to the counter to get the next word.
    mov eax, word [ebx] ; get the nth word into eax
    cmp eax, 9 ; check if the nth word is two-digit
    jng .loop ; if it is not two digit just loop

    add word[sum], eax ; if it is add it to sum
    jmp .loop ; and loop

endLoop:
    ret

  • Related