问题描述
我想将高级语言中的简单循环转换为汇编语言(适用于emu8086),例如,我有以下代码:
I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code:
for(int x = 0; x<=3; x++)
{
//Do something!
}
或
int x=1;
do{
//Do something!
}
while(x==1)
或
while(x==1){
//Do something
}
如何在emu8086中执行此操作?
How do I do this in emu8086?
推荐答案
For循环:
在C语言中循环:
For-loops:
For-loop in C:
for(int x = 0; x<=3; x++)
{
//Do something!
}
8086汇编器中的相同循环:
The same loop in 8086 assembler:
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次操作,但又不需要索引,那会更容易:
That is the loop if you need to access your index (cx). If you just wanna to something 0-3=4 times but you do not need the index, this would be easier:
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
循环执行
在C语言中执行循环操作
Do-while-loops
Do-while-loop in C:
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
循环时
在C中循环时
While-loops
While-loop in C:
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,因为它非常标准.对于其他循环条件,您可以记录自己的喜好.当然,请用您要在循环中执行的所有指令替换无操作指令.
For the for-loops you should take the cx-register because it is pretty much standard. 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.
这篇关于汇编语言中的While,Do While,For循环(emu8086)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!