a) AND with 0xAAAAAAAA b) OR with 0x00000007 c) AND with 0x00000007 d) OR with 0xFFFFFFFF e) XOR with 0xC0000000 f) AND with 0xFFFFFFF8
start: LOAD value ; get current value OUT 8 ; and write it out ADD one ; this will be the next value STORE value ; store it for next time through the loop SUB limit ; will be zero only when the next value is limit JLZ start ; not yet zero means we have more to do end: JUMP end ; only way to stop the program value: 0 one: 1 limit: 256 ; because we are always checking the next value
00000007 30000008 40000008 10000007 50000009 E0000000 C0000006 00000000 00000001 00000100
IN 0x100 STORE x IN 0x100 STORE y top: LOAD y ; if y == 0 JZ done ; return x STORE old_y ; else LOAD x MOD y STORE y ; next_y = x % y LOAD old_y STORE x ; next_x = old y JUMP top ; return gcd(next_x, next_y) done: LOAD x ; This is the real "return x" OUT 0x200 end: JMP end x: 0 y: 0 old_y: 0 ; yes we really need this
STORE t1 ; t1 holds a LOAD 0x30AA STORE t2 ; t2 holds x LOAD t1 STORE 0x30AA ; now 30AA holds original value of a LOAD t2 ; now accumulator holds x
There is an xor-style solution, too:
XOR 0x30AA ; acc holds a xor x STORE t ; t holds a xor x XOR 0x30AA ; acc holds a xor x xor x, which is a STORE 0x30AA ; 30AA holds a XOR t ; now accumulator holds a xor a xor x, which is x, all done!
JGZ 0x837BBE1 JZ 0x837BBE1
global start section .text start: mov rax, 0x2000004 ; system call for write mov rdi, 1 mov rsi, phrase ; address of string to output mov rdx, 19 ; number of bytes in string (UTF-8 encoded) syscall ; invoke OS to write mov rax, 0x2000001 ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke OS to exit section .data phrase: db '最近怎么样?', 10
Instruction | r8 | r9 |
---|---|---|
x | y | |
xor r8, r9 | x xor y | y |
xor r9, r8 | x xor y | y xor (x xor y) x |
xor r8, r9 | x xor y xor x y | x |