Author Topic: Loops within loops in assembly  (Read 8722 times)

0 Members and 1 Guest are viewing this topic.

Offline Hal9000Topic starter

  • Supreme Robot
  • *****
  • Posts: 338
  • Helpful? 0
Loops within loops in assembly
« on: February 24, 2007, 07:46:39 AM »
Hi again :)

I'm having a bit of trouble putting a loop within a loop in assembly language, as my system always hangs. Is anyone any good at this? The confusion comes when I think I only am able to use on register, CX

This is the code I would like to insert another loop into. I'm using a 8086 Microprocessor and MASM to compile.

Code: [Select]
        mov cx,0FFFFh
delay:
        nop
        nop
        nop
        loop delay
"The truth is, you can't hide from the truth, cos the truth is all there is" - Handsome Boy Modeling School

Offline hgordon

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 373
  • Helpful? 7
    • Surveyor Robotics Journal
Re: Loops within loops in assembly
« Reply #1 on: February 24, 2007, 09:14:17 AM »
That code will definitely hang your system.  You need a way to branch out of the loop, or you'll stay there forever. 

What are you trying to do - decrement the cx register in each iteration and leave the loop when cx hits 0 ?   If so, the code would look something like

delay:
    nop
    nop
    dec cx
    jnz delay

Not sure if dec and jnz are the right x86 instructions for "decrement" and "jump if not zero", but that's what you need


Surveyor Corporation
  www.surveyor.com

Offline Hal9000Topic starter

  • Supreme Robot
  • *****
  • Posts: 338
  • Helpful? 0
Re: Loops within loops in assembly
« Reply #2 on: February 24, 2007, 09:32:50 AM »
Well, maybe I should have given the bigger picture. I'm actually wanting just a long delay.

I have to use the LOOP instruction, and I just want to put a loop in a loop

LOOP automatically decrements CX until it is 0.

Thing is, when I try and use different registers other than CX for the loop, it won't work (because obviously, CX is for counting)

I've tried inserting 100 nops, but it won't take too many, and I know it looks rubbish. The loop within loop thing is the only way to go.

Code: [Select]
DELAY
    proc near
    mov cx,0FFFFh ;count value for the delay
    delay: nop ;twiddle fingers for a while
    nop ;again
    nop ;and again
    nop
    nop
    loop delay ;loop label = dec CX and then jnz label
    ret
DELAY endp

This code doesn't hang. I forgot to put in the ret command in the last code I sent, just because.

Cheers for taking a look :)
"The truth is, you can't hide from the truth, cos the truth is all there is" - Handsome Boy Modeling School

Offline hgordon

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 373
  • Helpful? 7
    • Surveyor Robotics Journal
Re: Loops within loops in assembly
« Reply #3 on: February 24, 2007, 11:06:45 AM »
Gotcha.  I didn't remember the existence of 'loop', but last time I looked at x86 asm was 1991.

        push cx ;  save cx and bx registers on the stack
        push bx
        mov cx,0FFFFh ; count value for outer loop
delay1: mov bx, 0100h ; count for inner loop
delay2: dec bx
         jnz delay2 ;   branch for inner loop
         dec cx
         jnz delay1  ;   branch for outer loop
         pop bx    ;  restore registers in correct order
         pop cx
         ret


push and pop are added in case the registers are used elsewhere in your code

« Last Edit: February 24, 2007, 11:10:16 AM by hgordon »
Surveyor Corporation
  www.surveyor.com

Offline Hal9000Topic starter

  • Supreme Robot
  • *****
  • Posts: 338
  • Helpful? 0
Re: Loops within loops in assembly
« Reply #4 on: February 24, 2007, 01:59:44 PM »
Oh, is it really like that?

Thankyou for your help! I've only been doing x86 assembly for 4 weeks!

:)
"The truth is, you can't hide from the truth, cos the truth is all there is" - Handsome Boy Modeling School

 


Get Your Ad Here