簡體   English   中英

匯編語言中的 While、Do While、For 循環 (emu8086)

[英]While, Do While, For loops in Assembly Language (emu8086)

我想將高級語言中的簡單循環轉換為匯編語言(對於 emu8086)說,我有這個代碼:

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

或者

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

或者

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

我如何在 emu8086 中做到這一點?

For 循環:

C中的for循環:

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

8086 匯編器中的相同循環:

        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

如果您需要訪問索引(cx),這就是循環。 如果你只是想要 0-3=4 次但你不需要索引,這會更容易:

        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

如果您只想以恆定次數執行非常簡單的指令,您還可以使用匯編指令,該指令只會對該指令進行硬核

times 4 nop

循環

C 中的 Do-while 循環:

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

匯編程序中的相同循環:

        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 循環

C中的while循環:

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

匯編程序中的相同循環:

        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 循環,您應該使用 cx-register,因為它幾乎是標准的。 對於其他循環條件,您可以使用自己喜歡的寄存器。 當然,用您想要在循環中執行的所有指令替換無操作指令。

Do{
   AX = 0
   AX = AX + 5
   BX = 0
   BX= BX+AX 
} While( AX != BX)

Do while 循環總是在每次迭代結束時檢查循環的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM