[英]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 中做到这一点?
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
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.