Society of Robots - Robot Forum
Software => Software => Topic started by: Hal9000 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.
mov cx,0FFFFh
delay:
nop
nop
nop
loop delay
-
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
-
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.
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 :)
-
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
-
Oh, is it really like that?
Thankyou for your help! I've only been doing x86 assembly for 4 weeks!
:)