37

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Glynn Bacanto
  • 439
  • 2
  • 6
  • 12
  • 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. – Jester Feb 23 '15 at 01:14
  • Nope! Only for emu8086! – Glynn Bacanto Feb 23 '15 at 01:19
  • http://stackoverflow.com/questions/8301137/how-to-loop-in-assembly-language?rq=1 – Stuart Siegler Feb 23 '15 at 01:27
  • But in emu8086, I can only use ax,bx,cx and dx! ecx does not exist? – Glynn Bacanto Feb 23 '15 at 01:29
  • 1
    Just lose the e from the register name. e stands for extended (I believe) - it indicates 32 bit-wide registers instead of 16 bits. – 500 - Internal Server Error Feb 23 '15 at 02:44
  • Enjoy the googly translation of [this article at my homepage](http://translate.google.com/translate?hl=&sl=de&tl=en&u=http%3A%2F%2Fdcla.rkhb.de%2Fschleifen.html). Replace `ECX` by `CX`. – rkhb Feb 23 '15 at 07:31
  • Related: [Why are loops always compiled into "do...while" style (tail jump)?](//stackoverflow.com/q/47783926) explains why `do{}while()` with the loop branch at the bottom is idiomatic, and how `for(){}` and `while(){}` loops should be implemented that way. – Peter Cordes Aug 20 '19 at 03:17
  • @Jester: so `if` and `goto` are enough to implement `while`? – Zimba Jul 10 '21 at 05:40
  • 1
    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;` – Jester Jul 10 '21 at 11:48

2 Answers2

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.

Maximilian Schier
  • 1,579
  • 14
  • 18
  • 4
    [don't use the `loop` instruction, it's slow on modern CPUs.](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently). – Peter Cordes Nov 08 '17 at 01:37
  • 1
    And in asm, use the `do{}while()` loop structure whenever possible, [for the same reason compilers do](https://stackoverflow.com/questions/47783926/why-are-loops-always-compiled-like-this): 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.) – Peter Cordes Jan 14 '18 at 04:41
  • The `for` loop code here is wrong. It should jump to the loop condition check before the loop's first iteration. – ecm Aug 28 '19 at 19:33
  • 1
    @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. – Peter Cordes Apr 09 '21 at 01:23
-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.

Dilan
  • 2,610
  • 7
  • 23
  • 33