Search code examples
assemblymasm

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


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][1]

[1]: https://i.sstatic.net/BZ2BM.png /// Edit , complety rewrote the code now it works thank youu everyone for the help <3


Solution

  • 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: dd 15, 16, 9, 8, 0xFFFFFFFF
    sum: dd 0x00000000
    

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

    start:
        push ebx
        mov edx, arr ; get the address of the array
        xor ecx, ecx
    .loop:
        mov eax, dword [edx] ; get the nth word into eax
        cmp eax, 0xFFFFFFFF ; check if we are at the end of list
        je endLoop ; if we are end the loop
        add edx, 4 ; add 2 to the pointer to get the next word.
        
        cmp eax, 9 ; check if the nth word is 1 digit
        jng .loop ; if it is 1 digit just loop
        cmp eax, 99 ; check if it is 3 digits
        jg .loop ; loop if it is
    
        add ecx, eax ; we have the two digit number, add it to sum
        jmp .loop ; and loop
    
    endLoop:
        mov dword [sum], ecx
        pop ebx
        ret