# Write out the (hex) textual representation of numbers. == code # instruction effective address register displacement immediate # . op subop mod rm32 base index scale r32 # . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes # convert the lowest nibble of eax to ascii and return it in the lowest byte of eax to-hex-char: # in/eax: int -> out/eax: int # no error checking; accepts argument in eax # if (eax <= 9) return eax + '0' 3d/compare-eax-with 0x9/imm32/9 7f/jump-if-> $to-hex-char:else/disp8 05/add-to-eax 0x30/imm32/0 c3/return $to-hex-char:else: # otherwise return eax + 'a' - 10 05/add-to-eax 0x57/imm32/a-10 c3/return append-byte-hex: # f: (addr stream byte), n: int # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # . save registers 50/push-eax # AL = convert upper nibble to hex 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax c1/shift 5/subop/logic-right 3/mod/direct 0/rm32/eax . . . . . 4/imm8 # shift eax right by 4 bits, while padding zeroes 25/and-eax 0xf/imm32 # . AL = to-hex-char(AL) e8/call to-hex-char/disp32 # append-byte(f, AL) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) # . . call e8/call append-byte/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # AL = convert lower nibble to hex 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax 25/and-eax 0xf/imm32 # . AL = to-hex-char(AL) e8/call to-hex-char/disp32 # append-byte(f, AL) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) # . . call e8/call append-byte/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp $append-byte-hex:end: # . restore registers 58/pop-to-eax # . epilogue 89/copy 3/mod/direct 4/rm
// "a type" "needed java_imports"
rn test-write-int32-hex: # - check that write-int32-hex prints the hex textual representation # setup # . clear-stream(_test-stream) # . . push args 68/push _test-stream/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # write-int32-hex(_test-stream, 0x8899aa) # . . push args 68/push 0x8899aa/imm32 68/push _test-stream/imm32 # . . call e8/call write-int32-hex/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-stream-equal(_test-stream, "0x008899aa", msg) # . . push args 68/push "F - test-write-int32-hex"/imm32 68/push "0x008899aa"/imm32 68/push _test-stream/imm32 # . . call e8/call check-stream-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # . end c3/return # . . vim:nowrap:textwidth=0