开发者

Assembly multiple conditional jumps

I can't organize jumps for the following pseudo code (to Assembly 8086):

array = ...numbers set...
cl = 2
num = 0
si = 0
while(si != 11)
{
   ax = 0
   al = arra开发者_开发百科y[si]

   // divide with assembly
   div cl
   if(ah = 0)
   {
      n+1
   }
   si+1
}

I've tried something like that:

.data
array db 0Fh, 45h, 0A1h, 78h, 0CFh, 0AAh, 8Fh, 19h ; Array
p db 0 ; Number of even numbers in the array
.code
xor si, si
xor cx, cx
mov cl, 2
again:
xor ax, ax 
mov al, array[si]
div cl
cmp ah, 0
je eq
inc si
cmp si, 11
jne again
eq:
inc p
inc si
cmp si, 11
jne again

Do you have any ideas how to fix this code?


One way would be to do the inc p right under the cmp ah, 0 jump and only jump over it if they're not equal:

...
cmp ah, 0
jne noteq
inc p
noteq:
inc si
cmp si, 11
jne again


Technically, what you are missing is a jump to the end just before the eq label. Currently, if the last iteration ends in the ah!=0 branch, it will fall through to the eq label, increase si to 12 and jump back to the again label. You could also reorganize the code such that only the inc p is jumped over, so both cases would use the same si comparison (similar to your pseudocode).

Note, if you only want to check for even/odd numbers, just testing the lowest bit is a simpler way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜