-4

I NEED TO SUM numbers 1,2,3,4,5,6,7,8,9,10 by using loop in 8086 assembly. Here my attempt:

    MOV AX,01h
    MOV CX,0ah
LABEL1:
    inc AX
    LOOP LABEL1
    HLT  
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
over
  • 29
  • 1
  • 1
  • 4

1 Answers1

4

You will need a container of some kind to store your sum into. You can choose the register DX for that.

First make sure to empty this register before starting the loop.
Then on each iteration of the loop you add the current value of AX to this register DX.

    mov     ax, 1
    mov     cx, 10
    xor     dx, dx    ;This puts zero in DX
Label1:
    add     dx, ax    ;This adds int turn 1, 2, 3, ... ,10 to DX
    inc     ax
    loop    Label1

Not sure if you're required to use the loop instruction, but an alternative loop uses AX for its loop control.

    mov     ax, 1
    cwd               ;This puts zero in DX because AX holds a positive number
Label1:
    add     dx, ax    ;This adds in turn 1, 2, 3, ... ,10 to DX
    inc     ax
    cmp     ax, 10
    jbe     Label1

A somewhat better loop adds the numbers from high to low. This way the cmp instruction is no longer needed.

    mov     ax, 10
    cwd               ;This puts zero in DX because AX holds a positive number
Label1:
    add     dx, ax    ;This adds in turn 10, 9, 8, ... ,1 to DX
    dec     ax
    jnz     Label1
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 3
    @over Both my loops will give 55 (decimal) as their result. Most probably the debugger that you use is displaying the result in **hexadecimal** where decimal 55 is shown as hexadecimal 37. (**3** x 16 + **7**) – Sep Roland Mar 13 '18 at 14:16