[英]While, Do While, For loops in Assembly Language (emu8086)
I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code:我想将高级语言中的简单循环转换为汇编语言(对于 emu8086)说,我有这个代码:
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?我如何在 emu8086 中做到这一点?
For-loop in C: C中的for循环:
for(int x = 0; x<=3; x++)
{
//Do something!
}
The same loop in 8086 assembler: 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
That is the loop if you need to access your index (cx).如果您需要访问索引(cx),这就是循环。 If you just wanna to something 0-3=4 times but you do not need the index, this would be easier:如果你只是想要 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
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-loop in C: C 中的 Do-while 循环:
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-loop in C: C中的while循环:
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 循环,您应该使用 cx-register,因为它几乎是标准的。 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.当然,用您想要在循环中执行的所有指令替换无操作指令。
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. Do while 循环总是在每次迭代结束时检查循环的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.