Gly*_*nto 18 assembly loops for-loop while-loop x86-16
我想将高级语言中的简单循环转换为汇编语言(对于emu8086)说,我有这样的代码:
for(int x = 0; x<=3; x++)
{
//Do something!
}
Run Code Online (Sandbox Code Playgroud)
要么
int x=1;
do{
//Do something!
}
while(x==1)
Run Code Online (Sandbox Code Playgroud)
要么
while(x==1){
//Do something
}
Run Code Online (Sandbox Code Playgroud)
我如何在emu8086中执行此操作?
Max*_*son 40
C中的For循环:
for(int x = 0; x<=3; x++)
{
//Do something!
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如果您需要访问索引(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
Run Code Online (Sandbox Code Playgroud)
如果你只是想要执行一个非常简单的指令,你也可以使用一个汇编程序指令,它只是硬核指令
times 4 nop
Run Code Online (Sandbox Code Playgroud)
C中的do-while-loop:
int x=1;
do{
//Do something!
}
while(x==1)
Run Code Online (Sandbox Code Playgroud)
汇编程序中的相同循环:
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
Run Code Online (Sandbox Code Playgroud)
在C中while循环:
while(x==1){
//Do something
}
Run Code Online (Sandbox Code Playgroud)
汇编程序中的相同循环:
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
Run Code Online (Sandbox Code Playgroud)
对于for循环,你应该使用cx寄存器,因为它非常标准.对于其他循环条件,您可以根据自己的喜好进行注册.当然,用你想在循环中执行的所有指令替换无操作指令.