Loops 汇编语言(emu8086)中的While、Do-While、For循环

Loops 汇编语言(emu8086)中的While、Do-While、For循环,loops,for-loop,assembly,while-loop,x86-16,Loops,For Loop,Assembly,While Loop,X86 16,我想将高级语言中的简单循环转换为汇编语言(对于emu8086),比如,我有以下代码: for(int x = 0; x<=3; x++) { //Do something! } 或 如何在emu8086中执行此操作 对于循环: 对于C中的循环: for(int x = 0; x<=3; x++) { //Do something! } int x=1; do{ //Do something! } while(x==1) while(x==1){

我想将高级语言中的简单循环转换为汇编语言(对于emu8086),比如,我有以下代码:

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

如何在emu8086中执行此操作

对于循环: 对于C中的循环:

for(int x = 0; x<=3; x++)
{
    //Do something!
}
int x=1;
do{
    //Do something!
}
while(x==1)
while(x==1){
    //Do something
}
如果需要访问索引(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
做while循环 在C中执行while循环:

for(int x = 0; x<=3; x++)
{
    //Do something!
}
int x=1;
do{
    //Do something!
}
while(x==1)
while(x==1){
    //Do something
}
汇编程序中的相同循环:

        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
        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
        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
While循环 C中的While循环:

for(int x = 0; x<=3; x++)
{
    //Do something!
}
int x=1;
do{
    //Do something!
}
while(x==1)
while(x==1){
    //Do something
}
汇编程序中的相同循环:

        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
        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
        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寄存器,因为它几乎是标准的。对于其他循环条件,您可以根据自己的喜好进行登记。当然,用您希望在循环中执行的所有指令替换无操作指令

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

Do while loop始终在每次迭代结束时检查循环的条件。

假设您已经知道如何在程序集中实现比较和条件跳转,请先使用
if
goto
重写代码和/或创建流程图。不!只适用于emu8086!但在emu8086中,我只能使用ax、bx、cx和dx!ecx不存在?只是从寄存器名中丢失e。e代表扩展(我相信)-它表示32位宽的寄存器,而不是16位。在asm中,尽可能使用
do{}while()
循环结构:循环中的指令越少,代码运行越快。(通常剥离运行零次检查比跳到循环底部要好,就像您在
while
循环中所做的那样。)这里的
for
循环代码是错误的。在循环的第一次迭代之前,它应该跳转到循环条件检查。@ecm:这并不是完全错误,它正在应用优化,
xPlease格式化您的代码