-1

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.stack.imgur.com/BZ2BM.png /// Edit , complety rewrote the code now it works thank youu everyone for the help <3

VIGEEDUN
  • 13
  • 3
  • 1
    Please don't post code as images, but as text instead. You can surround code as text with the triple back tick for proper code formatting. – Erik Eidt Oct 14 '22 at 16:36
  • 1
    Write the loop you want in pseudo code or better yet, in C. The idea is that it helps to know what you want the program to do before writing assembly code. Developing an algorithm directly in assembly is hard when you know neither the algorithm nor assembly. So, think about it in a language you know, then when you have that working take it to assembly. – Erik Eidt Oct 14 '22 at 16:42

1 Answers1

2

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

  • Thankkk youu for the help man, comments came in clutch for me to understand what's happening , had to change a few things because it comes out in syntax error if I use ur code but it helped so much thankk youuu – VIGEEDUN Oct 15 '22 at 10:58
  • @VIGEEDUN No problem, happy to help. The syntax errors are because I wrote the code on the StackOverflow page. Next time don't forget to copy and paste code into your question, otherwise, it is very hard to check the code. – Özgür Güzeldereli Oct 15 '22 at 11:05
  • Can I reach you on somewhere else I want to ask more questions about this code and I kind of want to ask some things about the syntax in assembly, like for example why am I not able to use the [] brackets in my code it always goes into syntax error , and using the word[ebx] also results in an error – VIGEEDUN Oct 15 '22 at 11:26
  • Okay I sent you an email, check when you have some free time thank you. – VIGEEDUN Oct 15 '22 at 11:30
  • This seems over-complicated (manually doing array indexing by adding integers to pointers), and inefficient vs. `mov ebx, arr` once outside the loop, and `movzx edx, word [ebx+ecx*2]` inside the loop, or better a pointer increment. Also, won't assemble: `mov eax, word [ebx]` and `add eax, word [ebx]` have an operand-size mismatch with the 32-bit (`dword`) register EAX. Since you do load it into a register, you should just do that once and check for `-1` against `AX`, not memory. – Peter Cordes Oct 16 '22 at 03:03
  • You also don't need to keep the sum in memory; it would be normal to keep it in a register. Also, you don't reject 3-digit or larger numbers, so it's actually adding all numbers >=10, regardless of number of digits. Also, this is NASM syntax, not like the picture of text @VIGEEDUN posted with MASM syntax, so if they're trying to port it to MASM they might have problems. (They should have copy/pasted code as text, and ideally tagged their question [masm].) Their code uses a DWORD array, like you'd expect for 32-bit code, not 16-bit words. – Peter Cordes Oct 16 '22 at 03:06
  • @PeterCordes I wrote the code in the StackOveflow page so there are probably a couple of errors. Thanks for the fixes – Özgür Güzeldereli Oct 16 '22 at 08:28
  • @PeterCordes is this better? – Özgür Güzeldereli Oct 16 '22 at 08:58
  • Yeah, that's a lot more reasonable. Still poor style (and performance) to update the `sum` in memory every iteration; that's what registers are for. And EBX is call-preserved in standard calling conventions. And the range check for `x>9 && x<100` could be [optimized](https://stackoverflow.com/questions/54974851/reverse-engineering-asm-using-sub-cmp-setbe-back-to-c-my-attempt-is-compili/54976091#54976091) to `lea`/`cmp`/ja`, but this is the obvious simplistic way to do it. [double condition checking in assembly](https://stackoverflow.com/q/5196527) – Peter Cordes Oct 16 '22 at 09:47
  • @PeterCordes I made it so that we only modify ```sum``` once at the end and also preserved ```ebx```. I did find that post about double condition checking while writing this but didn't use the one with ```sub/cmp/ja``` because the OP is a beginner in assembly. – Özgür Güzeldereli Oct 16 '22 at 10:06
  • Yeah, 2x cmp/jcc is a very reasonable way to do it in a beginner answer. Nice edit with the other changes. – Peter Cordes Oct 16 '22 at 10:09