0

I am new to Assembly. I'd like to produce a loop counter that sums up the first 10 decimal numbers (in 32-bit). Here is what I came up with. The code must run in MASM compiler. Should not output anything to the console but save the result in to a registry memory. Any thought how to achieve this? Here is what I have so far

         .386
         .model  flat

         .code
main proc
    mov        eax, 0   ; initialize the counter
FLP:
    mov        ebx, ecx
    add        ebx, eax
    sub        ebx, edx
    mov        ecx, ebx
    inc        eax
    cmp        eax, 10
    jle        FLP
main endp
end main

Solution

.model flat
;.data
.code

main PROC
    xor ecx, ecx 
    xor eax, eax
loop10:
    add eax, ecx
    inc ecx
    cmp ecx, 10
    jbe loop10
main ENDP 
END main
sec-social
  • 11
  • 1
  • 2
  • 1
    What do you mean by _"a registry memory"_? Are you talking about the CPU's general purpose registers, such as `eax`, `ebx`, etc? If so, you're already using those, so it's not clear what the problem is. – Michael Oct 09 '19 at 06:50
  • There's no reason to mess around with 16-bit registers in 32-bit code for this task. `add esi, bx` won't even assemble because the operands are different sizes. Anyway, you probably want to leave your return value in EAX, as per the standard calling convention. – Peter Cordes Oct 09 '19 at 09:07
  • 1
    `mov ax, ax`? Really? `add esi, bx`? Seriously? I am curious, how did you come up with that? – Mike Nakis Oct 09 '19 at 09:16
  • @MikeNakis, I made some changes. All, Peter Cordes, you're right. See updated code – sec-social Oct 09 '19 at 22:41
  • As a side note, it's usually better to use `xor` to zero a register instead of a `mov`. See https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and – Macmade Oct 09 '19 at 22:59

0 Answers0