Code implementation with "and" and "jg" or "jle" | as

I tried to solve the question shown below:

Just using both, and jg or jle, how can the following code be implemented?

if %eax > 4 jmp do else jmp l1 

of course, without changing the value of eax

This question is taken from a textbook, but no answer. Can you show me how to solve it?

EDIT: I tried:

  subl $4, %eax andl %eax, %eax jg do jmp l1 . . . do : addl $4, %eax . . . l1: addl $4, %eax 
+4
source share
2 answers

The following and and jle solves the problem (using NASM syntax):

 ; 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 

The danger here is that this is the part of the code used once since it irreversibly modifies 2 (or 3) variables.

+2
source

Is there something wrong with cmp ?

 cmp eax, 4 jg do jmp l1 

If jmp is not allowed, you can do it like this:

 cmp eax, 4 jg do jle l1 

If you were allowed to change eax (or use mov or push / pop ), the answer would be:

 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 
+1
source

Source: https://habr.com/ru/post/1386460/


All Articles