tags:

views:

1626

answers:

4

I have written the code so far as:

.code

main
 Clrscr

  mov dh,10            ;row 10

  mov dl,20            ;column 20

  call Gotoxy          ;locate cursor

  PromptForIntegers  

    WriteString       ;display string
    ReadInt           ;input integer
  ArraySum

    WriteString       ;display string
    WriteInt          ;display integer

DisplaySum  ENDP

END main

How do I get it to repeat the same steps three times using a loop, clearing the screen after each loop iteration?

A: 

Use the CX register to count the loops

mov cx, 3
startloop:
   cmp cx, 0
   jz endofloop
   push cx
loopy:
   Call ClrScr
   pop cx
   dec cx
   jmp startloop
endofloop:
   ; Loop ended
   ; Do what ever you have to do here

This simply loops around 3 times calling ClrScr, pushing the CX register onto the stack, comparing to 0, jumping if ZeroFlag is set then jump to endofloop. Notice how the contents of CX is pushed/popped on/off the stack to maintain the flow of the loop.

Hope this helps, Best regards, Tom.

tommieb75
a couple of optimizations: (1)you may use `jcxz label` instead of `cmp cx,0` and `jz label`. (2) you may use `loop label` instead of `dec cx` and `jnz label`
PA
@PA: Depending on your processor, `jcxz label` and `cmp/jz` are equivalent. Recent x86 processor use macro-fusion to combine cmp/jmp instructions into a single cycle instruction, essentially replicating `jcxz` behavior on the fly.
Jeff B
+1  A: 

You need to use conditional jmp commands. This isn't the same syntax as you're using; looks like MASM, but using GAS here's an example from some code I wrote to calculate gcd:

gcd_alg:
    subl    %ecx, %eax      /* a = a - c */
    cmpl    $0, %eax        /* if a == 0 */
    je      gcd_done        /* jump to end */
    cmpl    %ecx, %eax      /* if a < c */
    jl      gcd_preswap     /* swap and start over */
    jmp     gcd_alg         /* keep subtracting */

Basically, I compare two registers with the cmpl instruction (compare long). If it is less the JL (jump less) instruction jumps to the preswap location, otherwise it jumps back to the same label.

As for clearing the screen, that depends on the system you're using.

Ninefingers
+4  A: 
mov cx,3

loopstart:
   do stuff
   dec cx
   jnz loopstart
Arthur Kalliokoski
you may use `loop loopstart` instead of `dec cx` and `jnz loopstart` as long as `do stuff` preserves the cx register
PA
@PA: Preserving cx is necessary even if you don't use `loop`.
Jeff B
A: 

Yet another method is using the LOOP instruction:

mov  cx, 3

myloop:
    ; Your loop content

    loop myloop

The loop instruction automatically decrements cx, and only jumps if cx != 0. There are also LOOPE, and LOOPNE variants, if you want to do some additional check for your loop to break out early.

If you want to modify cx during your loop, make sure to push it onto the stack before the loop content, and pop it off after:

mov  cx, 3

myloop:
    push cx
    ; Your loop content
    pop  cx

    loop myloop
Jeff B
Thanks for the help