While, Do While, For loops in Assembly Language (emu8086)
Asked Answered
I

2

36

I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code:

 for(int x = 0; x<=3; x++)
 {
  //Do something!
 }

or

 int x=1;
 do{
 //Do something!
 }
 while(x==1)

or

 while(x==1){
 //Do something
 }

How do I do this in emu8086?

Insufficient answered 23/2, 2015 at 1:7 Comment(9)
Assuming you know how to implement comparisons and conditional jumps in assembly already, rewrite the code using if and goto first and/or create a flowchart.Struble
Nope! Only for emu8086!Insufficient
stackoverflow.com/questions/8301137/…Hallucinogen
But in emu8086, I can only use ax,bx,cx and dx! ecx does not exist?Insufficient
Just lose the e from the register name. e stands for extended (I believe) - it indicates 32 bit-wide registers instead of 16 bits.Photocomposition
Enjoy the googly translation of this article at my homepage. Replace ECX by CX.Helmer
Related: Why are loops always compiled into "do...while" style (tail jump)? explains why do{}while() with the loop branch at the bottom is idiomatic, and how for(){} and while(){} loops should be implemented that way.Besnard
@Jester: so if and goto are enough to implement while?Baugher
Yes, apart from whatever else you need in the body. For example a do-while is a single conditional jump start: { body }; if (condition) goto start;Struble
P
74

For-loops:

For-loop in C:

for(int x = 0; x<=3; x++)
{
    //Do something!
}

The same loop in 8086 assembler:

        xor cx,cx   ; cx-register is the counter, set to 0
loop1   nop         ; Whatever you wanna do goes here, should not change cx
        inc cx      ; Increment
        cmp cx,3    ; Compare cx to the limit
        jle loop1   ; Loop while less or equal

That is the loop if you need to access your index (cx). If you just wanna to something 0-3=4 times but you do not need the index, this would be easier:

        mov cx,4    ; 4 iterations
loop1   nop         ; Whatever you wanna do goes here, should not change cx
        loop loop1  ; loop instruction decrements cx and jumps to label if not 0

If you just want to perform a very simple instruction a constant amount of times, you could also use an assembler-directive which will just hardcore that instruction

times 4 nop

Do-while-loops

Do-while-loop in C:

int x=1;
do{
    //Do something!
}
while(x==1)

The same loop in assembler:

        mov ax,1
loop1   nop         ; Whatever you wanna do goes here
        cmp ax,1    ; Check wether cx is 1
        je loop1    ; And loop if equal

While-loops

While-loop in C:

while(x==1){
    //Do something
}

The same loop in assembler:

        jmp loop1   ; Jump to condition first
cloop1  nop         ; Execute the content of the loop
loop1   cmp ax,1    ; Check the condition
        je cloop1   ; Jump to content of the loop if met

For the for-loops you should take the cx-register because it is pretty much standard. For the other loop conditions you can take a register of your liking. Of course replace the no-operation instruction with all the instructions you wanna perform in the loop.

Pfister answered 21/3, 2015 at 17:47 Comment(4)
don't use the loop instruction, it's slow on modern CPUs..Besnard
And in asm, use the do{}while() loop structure whenever possible, for the same reason compilers do: code runs faster with fewer instructions inside the loop. (Usually peeling the run-zero-times check is better than jumping to the bottom of the loop like you're doing here in your while loop.)Besnard
The for loop code here is wrong. It should jump to the loop condition check before the loop's first iteration.Parmenides
@ecm: It's not strictly wrong, it's applying the optimization that x<=3 is known to be true on the first iteration, allowing that check to be skipped. That's 100% standard and idiomatic for counted loops in assembly when you know they will run at least once, because of fixed loop bounds. Other kinds of for loops may need to sometimes run 0 times, but this answer is talking about for loops that follow the idiomatic pattern for looping x from 0 .. n.Besnard
F
-4
Do{
   AX = 0
   AX = AX + 5
   BX = 0
   BX= BX+AX 
} While( AX != BX)

Do while loop always checks the loop the condition at the end of each iteration.

Fiester answered 9/4, 2021 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.