使用“and”和“jg”或“jle”|来实现代码怎么样

时间:2011-12-15 07:10:23

标签: assembly x86

我试图解决下面显示的问题;

通过使用and和jg或jle,如何实现以下代码?

    if %eax > 4 
        jmp do
    else 
        jmp l1

当然不会改变eax的价值

这个问题取自教科书,但没有答案。你能告诉我如何解决它吗?

编辑: 我试过了;

     subl $4, %eax
     andl %eax, %eax
     jg  do
     jmp l1
      .
      .
      .
     do :
     addl $4, %eax
      .
      .
      .
     l1:
     addl $4, %eax

2 个答案:

答案 0 :(得分:2)

以下andjle序列解决了问题(使用了NASM语法):

; start execution here if eax is treated as an unsigned integer
unsigned_eax:
    and     dword [eighty - 3], eax
    and     dword [eighty], 80H
    jle     signed_eax ; jumps if eax <= 7FFFFFFFh, continues otherwise
    and     dword [eighty], 0
    jle     do ; jumps if eax > 7FFFFFFFh = always

; start execution here if eax is treated as a signed integer
signed_eax:
    and     dword [FFFFFFFC], eax
    jle     l1 ; jumps if eax < 4, continues if eax >= 4
    and     dword [FFFFFFFB], eax
    jle     l1 ; jumps if eax = 4, continues if eax > 4
    and     dword [zero], 0 ; this and the next instruction simulate "jmp do"
    jle     do
do:
    ; some code
l1:
    ; some code

FFFFFFFC    dd 0FFFFFFFCH
FFFFFFFB    dd 0FFFFFFFBH
zero        dd 0
eighty      dd 80H

这里需要注意的是,这是一次使用一次的代码,因为它不可逆转地修改了2(或3)个变量。

答案 1 :(得分:1)

cmp出了什么问题?

cmp eax, 4
jg do
jmp l1

如果不允许使用jmp,您可以这样做:

cmp eax, 4
jg do
jle l1

如果您被允许修改eax(或使用movpush / pop),答案将是:

and eax, eax // This line and the one below is to be removed if the value is unsigned
jle l1
and eax, 0xFFFFFFFC // not 3
jle l1
jg do