I am having a hard time grasping the concepts of assembly language. I am given this sample code:
ploop: mov ax, 0201h
add al, ah
cmp al, 5
jb ploop
eloop:
The hex value at the end of the loop is 0205, but I'm not sure I understand why.
For the first line, we are moving 0201 into ax, so al = 01 and ah = 02. Then you add ah to al, making al = 03. We compare al to 5 and since 3 < 5, it fits jb and goes through the ploop again. We go through all the steps and at cmp al = 05 == 5, so it no longer fits jb.
Is this the right way of looking at it?
Almost correct. Except you probably want it to look like the following instead:
mov ax, 0201h
ploop: add al, ah
cmp al, 5
jb ploop
eloop:
As it will otherwise go into an infinite loop as al
and ah
are overwritten in each loop iteration.
I would almost bet you've transcribed the code incorrectly. As it stands, you have an infinite loop. It needs to be more like this:
mov ax, 0201h
ploop: add al, ah
cmp al, 5
jb ploop
eloop:
As you posted it, ax
is being re-loaded with 0201h
at the beginning of each iteration of the loop. You're then adding the 02 in ah
to the 01 in al
. That'll give 3. You compare that to 5 and if it's less than (obviously it always will be) you execute the loop again.
With the label moved, we start with 02 in ah
and 01 in al
. At each iteration of the loop, however, we add 02 to the current content of al
, so it will follow the sequence 1, 3, 5. At each iteration we compare its content to 5, and continue the loop if and only if it's less than (viewed as an unsigned), so the loop with execute three iterations, then stop with al = 5.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.