我想把高级语言中的简单循环转换成汇编语言(对于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中做到这一点?
发布于 2015-03-22 01:47:24
For-loops:
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 nopDo-while-循环
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 equalWhile循环
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,因为它几乎是标准的。对于其他循环条件,您可以记录您的喜好。当然,将无操作指令替换为您希望在循环中执行的所有指令。
发布于 2021-04-09 08:06:30
Do{
AX = 0
AX = AX + 5
BX = 0
BX= BX+AX
} While( AX != BX)Do while循环总是在每次迭代结束时检查循环的条件。
https://stackoverflow.com/questions/28665528
复制相似问题