Ik wil eenvoudige lussen in talen op hoog niveau converteren naar assembler (voor emu8086) zeg, ik heb deze code:
for(int x = 0; x<=3; x++)
{
//Do something!
}
of
int x=1;
do{
//Do something!
}
while(x==1)
of
while(x==1){
//Do something
}
Hoe doe ik dit in emu8086?
Antwoord 1, autoriteit 100%
For-loops:
For-loop in C:
for(int x = 0; x<=3; x++)
{
//Do something!
}
Dezelfde lus 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
Dat is de lus als je toegang nodig hebt tot je index (cx). Als je iets 0-3=4 keer wilt doen, maar je hebt de index niet nodig, dan is dit makkelijker:
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
Als je gewoon een heel eenvoudige instructie een constant aantal keren wilt uitvoeren, kun je ook een assembler-richtlijn gebruiken die die instructie gewoon hard maakt
times 4 nop
Doe-terwijl-loops
Doe-terwijl-loop in C:
int x=1;
do{
//Do something!
}
while(x==1)
Dezelfde lus 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-lussen
While-lus in C:
while(x==1){
//Do something
}
Dezelfde lus 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
Voor de voor-lussen moet u het CX-register nemen omdat het vrijwel standaard is. Voor de andere lusomstandigheden kunt u een register van uw smaak nemen. Vervang natuurlijk de instructies van de no-operation met alle instructies die u in de lus wilt uitvoeren.
Antwoord 2
Do{
AX = 0
AX = AX + 5
BX = 0
BX= BX+AX
} While( AX != BX)
Doe, terwijl de lus altijd de lus controleert aan het einde van elke iteratie.