[英]How to exit a loop in assembly
I have a loop with a couple of conditions, which means that when the loop is finished, it will proceed to go through the remaining loop segment. 我有一个带有几个条件的循环,这意味着当循环结束时,它将继续遍历其余的循环段。 How can I force the program to skip past the remaining loop segment even if ecx is already at 0? 即使ecx已经为0,如何强制程序跳过剩余的循环段?
Loops and conditions are created with LABELS, COMPARISONS and JUMPS: 使用标签,比较和跳转创建循环和条件:
xor ecx,ecx ;ECX = 0
mov eax,8 ;EAX = 8
mov ebx,4 ;EBX = 4
START_LOOP:
sub eax,ebx ;EAX = EAX - EBX
cmp eax,ecx ;compare EAX and ECX
jne START_LOOP ;if EAX != ECX, jump back and loop
;When EAX = ECX, execution continues pas the jump
You can loop a number of times using a loop index that we usually put in ECX: 您可以使用通常在ECX中放入的循环索引来循环多次:
xor ecx,ecx ;ECX = 0
mov eax,2 ;EAX = 2
mov ebx,2 ;EBX = 2
START_LOOP:
add eax,ebx ;EAX = EAX + EBX
inc ecx ;ECX = ECX + 1
cmp ecx,5 ;compare ECX and 5
jne START_LOOP ;if ECX != 5 jump back and loop
;When ECX == 5, execution continues pas the jump
Last, you can use conditions inside a loop using different labels: 最后,您可以使用不同的标签在循环内使用条件:
xor ecx,ecx ;ECX = 0
mov eax,2 ;EAX = 2
xor ebx,ebx ;EBX = 0
START_LOOP:
cmp eax,ebx ;compare EAX and EBX
jle CONTINUE ;if EAX <= EBX jump to the CONTINUE label
inc ebx ;else EBX = EBX + 1
jmp START_LOOP ;JUMP back to the start (until EBX>=EAX)
;You'll never get past this jump until the condition in reached
CONTINUE:
add eax,ebx ;EAX = EAX + EBX
inc ecx ;ECX = ECX + 1
cmp ecx,5 ;compare ECX and 5
jne START_LOOP ;if ECX != 5 jump back and loop
;When ECX == 5, execution continues pas the jump
You'll have to jump past them. 您必须跳过它们。 That's the only flow control you get. 那是您获得的唯一流控制。 Try to emulate the structure you would use in a higher level language, though, so as to avoid making spaghetti code. 但是,尝试模拟将在高级语言中使用的结构,以免生成意大利面条式代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.