4644
This commit is contained in:
parent
f872f7c726
commit
d25e51dc0f
|
@ -15,15 +15,15 @@ write_stderr: # s : (address array byte) -> <void>
|
|||
53/push-EBX
|
||||
# write(2/stderr, (data) s+4, (size) *s)
|
||||
# fd = 2 (stderr)
|
||||
bb/copy . . . . . . . 2/imm32 # copy 2 to EBX
|
||||
bb/copy . . . . . . . 2/imm32 # copy to EBX
|
||||
# x = s+4
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add 4 to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add to ECX
|
||||
# size = *s
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX
|
||||
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
||||
# call write()
|
||||
b8/copy . . . . . . . 4/imm32/write # copy 1 to EAX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
# restore registers
|
||||
5b/pop-to-EBX
|
||||
|
|
|
@ -24,7 +24,7 @@ check_ints_equal: # (a : int, b : int, msg : (address array byte)) -> boolean
|
|||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# return
|
||||
eb/jump $check_ints_equal:end/disp8
|
||||
# else:
|
||||
|
@ -37,14 +37,14 @@ $check_ints_equal:else:
|
|||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# print newline
|
||||
# push args
|
||||
68/push Newline/imm32
|
||||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
$check_ints_equal:end:
|
||||
# restore registers
|
||||
5b/pop-to-EBX
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
e8/call run_tests/disp32 # 'run_tests' is a function created automatically by SubX. It calls all functions that start with 'test_'.
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
|
||||
|
@ -60,9 +60,9 @@ kernel_string_equal: # s : null-terminated ascii string, benchmark : length-pre
|
|||
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
||||
# initialize benchmark data into ESI
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
|
||||
81 0/subop/add 3/mod/direct 6/rm32/ESI . . . . . 4/imm32 # add 4 to ESI
|
||||
81 0/subop/add 3/mod/direct 6/rm32/ESI . . . . . 4/imm32 # add to ESI
|
||||
# initialize loop counter i into ECX
|
||||
b9/copy . . . . . . . 0/imm32/exit # copy 1 to ECX
|
||||
b9/copy . . . . . . . 0/imm32/exit # copy to ECX
|
||||
# while (i/ECX < n/EDX)
|
||||
$kernel_string_loop:
|
||||
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
|
||||
|
@ -73,7 +73,7 @@ $kernel_string_loop:
|
|||
bb/copy 0/imm32 # clear EBX
|
||||
8a/copy 0/mod/indirect 6/rm32/ESI . . . 3/r32/EBX . . # copy byte at *ESI to lower byte of EBX
|
||||
# if (c1 == 0) return false
|
||||
3d/compare . . . . . . . 0/imm32 # compare EAX with 0
|
||||
3d/compare-EAX 0/imm32
|
||||
74/jump-if-equal $kernel_string_fail/disp8
|
||||
# if (c1 != c2) return false
|
||||
39/compare 3/mod/direct 0/rm32/EAX . . . 3/r32/EBX . . # compare EAX with EBX
|
||||
|
@ -88,13 +88,13 @@ $kernel_string_break:
|
|||
# if (*s/EDI == 0) return true
|
||||
b8/copy 0/imm32 # clear EAX
|
||||
8a/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy byte at *EDI to lower byte of EAX
|
||||
81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32 # compare EAX with 0
|
||||
3d/compare-EAX 0/imm32
|
||||
75/jump-if-not-equal $kernel_string_fail/disp8
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
eb/jump $kernel_string_end/disp8
|
||||
# return false
|
||||
$kernel_string_fail:
|
||||
b8/copy . . . . . . . 0/imm32 # copy 0 to EAX
|
||||
b8/copy . . . . . . . 0/imm32 # copy to EAX
|
||||
|
||||
$kernel_string_end:
|
||||
# restore registers
|
||||
|
@ -118,7 +118,7 @@ test_compare_null_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 1, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_null_kernel_string_with_empty_array"/imm32
|
||||
|
@ -127,7 +127,7 @@ test_compare_null_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_null_kernel_string_with_non_empty_array:
|
||||
|
@ -138,7 +138,7 @@ test_compare_null_kernel_string_with_non_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_null_kernel_string_with_non_empty_array"/imm32
|
||||
|
@ -147,7 +147,7 @@ test_compare_null_kernel_string_with_non_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_equal_array:
|
||||
|
@ -158,7 +158,7 @@ test_compare_kernel_string_with_equal_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 1, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -167,7 +167,7 @@ test_compare_kernel_string_with_equal_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_inequal_array:
|
||||
|
@ -178,7 +178,7 @@ test_compare_kernel_string_with_inequal_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -187,7 +187,7 @@ test_compare_kernel_string_with_inequal_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_empty_array:
|
||||
|
@ -198,7 +198,7 @@ test_compare_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -207,7 +207,7 @@ test_compare_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_shorter_array:
|
||||
|
@ -218,7 +218,7 @@ test_compare_kernel_string_with_shorter_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_shorter_array"/imm32
|
||||
|
@ -227,7 +227,7 @@ test_compare_kernel_string_with_shorter_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_longer_array:
|
||||
|
@ -238,7 +238,7 @@ test_compare_kernel_string_with_longer_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_longer_array"/imm32
|
||||
|
@ -247,7 +247,7 @@ test_compare_kernel_string_with_longer_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
== data
|
||||
|
|
Binary file not shown.
|
@ -25,10 +25,10 @@
|
|||
# call
|
||||
e8/call abort/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# exit(0)
|
||||
bb/copy . . . . . . . 0/imm32 # copy 0 to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
bb/copy . . . . . . . 0/imm32 # copy to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
## helpers
|
||||
|
@ -42,10 +42,10 @@ abort: # s : (address array byte) -> <void>
|
|||
# call
|
||||
e8/call error/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# exit(1)
|
||||
bb/copy . . . . . . . 1/imm32 # copy 1 to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# print out "Error: #{s} expected\n" to stderr
|
||||
|
@ -56,28 +56,28 @@ error: # s : (address array byte) -> <void>
|
|||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# write_stderr(s)
|
||||
# push args
|
||||
ff 6/subop/push 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . . 4/disp8 . # push *(ESP+4)
|
||||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# write_stderr(" expected")
|
||||
# push args
|
||||
68/push " expected"/imm32
|
||||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# write_stderr("\n")
|
||||
# push args
|
||||
68/push Newline/imm32
|
||||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# end
|
||||
c3/return
|
||||
|
||||
|
@ -92,15 +92,15 @@ write_stdout: # s : (address array byte) -> <void>
|
|||
53/push-EBX
|
||||
# write(1/stdout, (data) s+4, (size) *s)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy . . . . . . . 1/imm32 # copy 1 to EBX
|
||||
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
||||
# x = s+4
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add 4 to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add to ECX
|
||||
# size = *s
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX
|
||||
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
||||
# call write()
|
||||
b8/copy . . . . . . . 4/imm32/write # copy 1 to EAX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
# restore registers
|
||||
5b/pop-to-EBX
|
||||
|
|
Binary file not shown.
|
@ -23,7 +23,7 @@
|
|||
# prolog
|
||||
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
||||
# if (argc > 1)
|
||||
81 7/subop/compare 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . . 0/disp8 1/imm32 # compare *EBP with 1
|
||||
81 7/subop/compare 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . . 0/disp8 1/imm32 # compare *EBP
|
||||
7e/jump-if-lesser-or-equal $run_main/disp8
|
||||
# and if (argv[1] == "test")
|
||||
# push args
|
||||
|
@ -32,9 +32,9 @@
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# check result
|
||||
3d/compare . . . . . . . 1/imm32 # compare EAX with 1
|
||||
3d/compare-EAX 1/imm32
|
||||
75/jump-if-not-equal $run_main/disp8
|
||||
# then
|
||||
e8/call run_tests/disp32
|
||||
|
@ -46,11 +46,11 @@ $run_main:
|
|||
# EAX <- call
|
||||
e8/call factorial/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
$main_exit:
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# factorial(n)
|
||||
|
@ -60,18 +60,18 @@ factorial:
|
|||
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
||||
53/push-EBX
|
||||
# initialize EAX to 1 (base case)
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
# if (n <= 1) jump exit
|
||||
81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 1/imm32 # compare *(EBP+8) with 1
|
||||
81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 1/imm32 # compare *(EBP+8)
|
||||
7e/jump-if-<= $factorial:exit/disp8
|
||||
# EBX: n-1
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none 3/r32/EBX 8/disp8 . # copy *(EBP+8) to EBX
|
||||
81 5/subop/subtract 3/mod/direct 3/rm32/EBX . . . . . 1/imm32 # subtract 1 from EBX
|
||||
81 5/subop/subtract 3/mod/direct 3/rm32/EBX . . . . . 1/imm32 # subtract from EBX
|
||||
# EAX: factorial(n-1)
|
||||
53/push-EBX
|
||||
e8/call . . . . . . factorial/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# return n * factorial(n-1)
|
||||
f7 4/subop/multiply 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none 8/disp8 . # multiply *(EBP+8) into EAX
|
||||
# TODO: check for overflow
|
||||
|
@ -89,7 +89,7 @@ test_factorial:
|
|||
# call
|
||||
e8/call factorial/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# check_ints_equal(EAX, 120, failure message)
|
||||
# push args
|
||||
68/push "F - test_factorial"/imm32
|
||||
|
@ -98,7 +98,7 @@ test_factorial:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
# end
|
||||
c3/return
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
# 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
|
||||
bb/copy 2a/imm32 # copy 42 to EBX
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
b8/copy 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# vim:ft=subx
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
# exit(EAX)
|
||||
$exit:
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# compare two null-terminated ascii strings
|
||||
|
@ -47,7 +47,7 @@ $argv_loop:
|
|||
bb/copy 0/imm32 # clear EBX
|
||||
8a/copy 0/mod/indirect 2/rm32/EDX . . . 3/r32/EBX . . # copy byte at *EDX to lower byte of EBX
|
||||
# if (c1 == 0) break
|
||||
3d/compare . . . . . . . 0/imm32 # compare EAX with 0
|
||||
3d/compare-EAX 0/imm32
|
||||
74/jump-if-equal $argv_break/disp8
|
||||
# if (c1 != c2) return false
|
||||
39/compare 3/mod/direct 0/rm32/EAX . . . 3/r32/EBX . . # compare EAX with EBX
|
||||
|
@ -59,11 +59,11 @@ $argv_loop:
|
|||
eb/jump $argv_loop/disp8
|
||||
$argv_break:
|
||||
# if (c2 == 0) return true
|
||||
81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0/imm32 # compare EBX with 0
|
||||
81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0/imm32 # compare EBX
|
||||
75/jump-if-not-equal $argv_fail/disp8
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
c3/return
|
||||
# return false
|
||||
$argv_fail:
|
||||
b8/copy . . . . . . . 0/imm32 # copy 0 to EAX
|
||||
b8/copy . . . . . . . 0/imm32 # copy to EAX
|
||||
c3/return
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
e8/call run_tests/disp32 # 'run_tests' is a function created automatically by SubX. It calls all functions that start with 'test_'.
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
|
||||
|
@ -60,9 +60,9 @@ kernel_string_equal: # s : null-terminated ascii string, benchmark : length-pre
|
|||
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
||||
# initialize benchmark data into ESI
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
|
||||
81 0/subop/add 3/mod/direct 6/rm32/ESI . . . . . 4/imm32 # add 4 to ESI
|
||||
81 0/subop/add 3/mod/direct 6/rm32/ESI . . . . . 4/imm32 # add to ESI
|
||||
# initialize loop counter i into ECX
|
||||
b9/copy . . . . . . . 0/imm32/exit # copy 1 to ECX
|
||||
b9/copy . . . . . . . 0/imm32/exit # copy to ECX
|
||||
# while (i/ECX < n/EDX)
|
||||
$kernel_string_loop:
|
||||
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
|
||||
|
@ -73,7 +73,7 @@ $kernel_string_loop:
|
|||
bb/copy 0/imm32 # clear EBX
|
||||
8a/copy 0/mod/indirect 6/rm32/ESI . . . 3/r32/EBX . . # copy byte at *ESI to lower byte of EBX
|
||||
# if (c1 == 0) return false
|
||||
3d/compare . . . . . . . 0/imm32 # compare EAX with 0
|
||||
3d/compare-EAX 0/imm32
|
||||
74/jump-if-equal $kernel_string_fail/disp8
|
||||
# if (c1 != c2) return false
|
||||
39/compare 3/mod/direct 0/rm32/EAX . . . 3/r32/EBX . . # compare EAX with EBX
|
||||
|
@ -88,13 +88,13 @@ $kernel_string_break:
|
|||
# if (*s/EDI == 0) return true
|
||||
b8/copy 0/imm32 # clear EAX
|
||||
8a/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy byte at *EDI to lower byte of EAX
|
||||
81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32 # compare EAX with 0
|
||||
81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32 # compare EAX
|
||||
75/jump-if-not-equal $kernel_string_fail/disp8
|
||||
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
eb/jump $kernel_string_end/disp8
|
||||
# return false
|
||||
$kernel_string_fail:
|
||||
b8/copy . . . . . . . 0/imm32 # copy 0 to EAX
|
||||
b8/copy . . . . . . . 0/imm32 # copy to EAX
|
||||
|
||||
$kernel_string_end:
|
||||
# restore registers
|
||||
|
@ -118,7 +118,7 @@ test_compare_null_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 1, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_null_kernel_string_with_empty_array"/imm32
|
||||
|
@ -127,7 +127,7 @@ test_compare_null_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_null_kernel_string_with_non_empty_array:
|
||||
|
@ -138,7 +138,7 @@ test_compare_null_kernel_string_with_non_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_null_kernel_string_with_non_empty_array"/imm32
|
||||
|
@ -147,7 +147,7 @@ test_compare_null_kernel_string_with_non_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_equal_array:
|
||||
|
@ -158,7 +158,7 @@ test_compare_kernel_string_with_equal_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 1, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -167,7 +167,7 @@ test_compare_kernel_string_with_equal_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_inequal_array:
|
||||
|
@ -178,7 +178,7 @@ test_compare_kernel_string_with_inequal_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0, msg)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -187,7 +187,7 @@ test_compare_kernel_string_with_inequal_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_empty_array:
|
||||
|
@ -198,7 +198,7 @@ test_compare_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_equal_array"/imm32
|
||||
|
@ -207,7 +207,7 @@ test_compare_kernel_string_with_empty_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_shorter_array:
|
||||
|
@ -218,7 +218,7 @@ test_compare_kernel_string_with_shorter_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_shorter_array"/imm32
|
||||
|
@ -227,7 +227,7 @@ test_compare_kernel_string_with_shorter_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
test_compare_kernel_string_with_longer_array:
|
||||
|
@ -238,7 +238,7 @@ test_compare_kernel_string_with_longer_array:
|
|||
# call
|
||||
e8/call kernel_string_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# call check_ints_equal(EAX, 0)
|
||||
# push args
|
||||
68/push "F - test_compare_kernel_string_with_longer_array"/imm32
|
||||
|
@ -247,7 +247,7 @@ test_compare_kernel_string_with_longer_array:
|
|||
# call
|
||||
e8/call check_ints_equal/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
||||
c3/return
|
||||
|
||||
## helpers
|
||||
|
@ -272,7 +272,7 @@ check_ints_equal: # (a : int, b : int, msg : (address array byte)) -> boolean
|
|||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# return
|
||||
eb/jump $check_ints_equal:end/disp8
|
||||
# else:
|
||||
|
@ -285,14 +285,14 @@ $check_ints_equal:else:
|
|||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
# print newline
|
||||
# push args
|
||||
68/push Newline/imm32
|
||||
# call
|
||||
e8/call write_stderr/disp32
|
||||
# discard arg
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
$check_ints_equal:end:
|
||||
# restore registers
|
||||
5b/pop-to-EBX
|
||||
|
@ -313,15 +313,15 @@ write_stderr: # s : (address array byte) -> <void>
|
|||
53/push-EBX
|
||||
# write(2/stderr, (data) s+4, (size) *s)
|
||||
# fd = 2 (stderr)
|
||||
bb/copy . . . . . . . 2/imm32 # copy 2 to EBX
|
||||
bb/copy . . . . . . . 2/imm32 # copy to EBX
|
||||
# x = s+4
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add 4 to ECX
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add to ECX
|
||||
# size = *s
|
||||
8b/copy 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX
|
||||
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
||||
# call write()
|
||||
b8/copy . . . . . . . 4/imm32/write # copy 1 to EAX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
# restore registers
|
||||
5b/pop-to-EBX
|
||||
|
|
|
@ -13,15 +13,15 @@
|
|||
|
||||
# mmap(0x1000)
|
||||
bb/copy . . . . . . . mmap_new_segment/imm32 # copy to EBX
|
||||
b8/copy . . . . . . . 0x5a/imm32/mmap # copy 8 to EAX
|
||||
b8/copy . . . . . . . 0x5a/imm32/mmap # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# store to *EAX
|
||||
c7/copy 0/mod/direct 0/rm32/EAX . . . . . 0x34/imm32 # copy 0x34 to *EAX
|
||||
c7/copy 0/mod/direct 0/rm32/EAX . . . . . 0x34/imm32 # copy to *EAX
|
||||
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
== data
|
||||
|
|
Binary file not shown.
|
@ -11,10 +11,10 @@
|
|||
# instruction effective address operand 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
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
81 0/subop/add 3/mod/direct 3/rm32/EBX 1/imm32 # add 1 to EBX
|
||||
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
||||
43/inc-EBX
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# vim:ft=subx
|
||||
|
|
Binary file not shown.
|
@ -12,24 +12,24 @@
|
|||
# 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
|
||||
# result: EBX = 0
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
bb/copy 0/imm32 # copy to EBX
|
||||
# counter: ECX = 1
|
||||
b9/copy 1/imm32 # copy 1 to ECX
|
||||
b9/copy 1/imm32 # copy to ECX
|
||||
|
||||
$loop:
|
||||
# while (counter <= 10)
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ECX 0xa/imm32 # compare ECX, 10/imm
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ECX 0xa/imm32 # compare ECX
|
||||
7f/jump-if-greater $exit/disp8
|
||||
# result += counter
|
||||
01/add 3/mod/direct 3/rm32/EBX 1/r32/ECX # add ECX to EBX
|
||||
# ++counter
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ECX 1/imm32 # add 1 to ECX
|
||||
41/inc-ECX
|
||||
# loop
|
||||
eb/jump $loop/disp8
|
||||
|
||||
$exit:
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
b8/copy 1/imm32 # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# vim:ft=subx:nowrap
|
||||
|
|
|
@ -11,28 +11,28 @@
|
|||
|
||||
# read(stdin, x, 1)
|
||||
# fd = 0 (stdin)
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
bb/copy 0/imm32 # copy to EBX
|
||||
# initialize x (location to write result to)
|
||||
b9/copy x/imm32 # copy to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy to EDX
|
||||
# read(fd, x, size)
|
||||
b8/copy 3/imm32/read # copy 3 to EAX
|
||||
b8/copy 3/imm32/read # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# write(stdout, x, 1)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
bb/copy 1/imm32 # copy to EBX
|
||||
# initialize x (location to read from)
|
||||
b9/copy x/imm32 # copy to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy 4/imm32/write # copy 4 to EAX
|
||||
b8/copy 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
== data
|
||||
|
|
|
@ -11,32 +11,32 @@
|
|||
|
||||
# main:
|
||||
# allocate x on the stack
|
||||
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # subtract 4 bytes from ESP
|
||||
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # subtract from ESP
|
||||
|
||||
# read(stdin, x, 1)
|
||||
# fd = 0 (stdin)
|
||||
bb/copy . . . . . . . 0/imm32 # copy 0 to EBX
|
||||
bb/copy . . . . . . . 0/imm32 # copy to EBX
|
||||
# initialize x (location to write result to)
|
||||
8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 1/r32/ECX 4/disp8 . # copy ESP+4 to ECX
|
||||
# size = 1 character
|
||||
ba/copy . . . . . . . 1/imm32 # copy 1 to EDX
|
||||
ba/copy . . . . . . . 1/imm32 # copy to EDX
|
||||
# read(fd, x, size)
|
||||
b8/copy . . . . . . . 3/imm32/read # copy 3 to EAX
|
||||
b8/copy . . . . . . . 3/imm32/read # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# write(stdout, x, 1)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy . . . . . . . 1/imm32 # copy 1 to EBX
|
||||
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
||||
# initialize x (location to read from)
|
||||
8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 1/r32/ECX 4/disp8 . # copy ESP+4 to ECX
|
||||
# size = 1 character
|
||||
ba/copy . . . . . . . 1/imm32 # copy 1 to EDX
|
||||
ba/copy . . . . . . . 1/imm32 # copy to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy . . . . . . . 4/imm32/write # copy 4 to EAX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# exit(EBX)
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# vim:ft=subx:nowrap
|
||||
|
|
|
@ -12,17 +12,17 @@
|
|||
|
||||
# write(stdout, x, size)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
bb/copy 1/imm32 # copy to EBX
|
||||
# initialize x (location to write result to)
|
||||
b9/copy x/imm32 # copy to ECX
|
||||
# initialize size
|
||||
8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX size/disp32 # copy *size to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy 4/imm32/write # copy 4 to EAX
|
||||
b8/copy 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
== data
|
||||
|
|
|
@ -18,14 +18,14 @@
|
|||
# creat(filename)
|
||||
bb/copy . . . . . . . filename/imm32 # copy to EBX
|
||||
b9/copy . . . . . . . 0x180/imm32/fixed-perms # copy to ECX
|
||||
b8/copy . . . . . . . 8/imm32/creat # copy 8 to EAX
|
||||
b8/copy . . . . . . . 8/imm32/creat # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# stream = open(filename, O_WRONLY, 0) # we can't use 'fd' because it looks like a hex byte
|
||||
bb/copy . . . . . . . filename/imm32 # copy to EBX
|
||||
b9/copy . . . . . . . 1/imm32/wronly # copy 1 to ECX
|
||||
ba/copy . . . . . . . 0x180/imm32/fixed-perms # copy 0 to EDX
|
||||
b8/copy . . . . . . . 5/imm32/open # copy 5 to EAX
|
||||
b9/copy . . . . . . . 1/imm32/wronly # copy to ECX
|
||||
ba/copy . . . . . . . 0x180/imm32/fixed-perms # copy to EDX
|
||||
b8/copy . . . . . . . 5/imm32/open # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
# save stream
|
||||
bb/copy . . . . . . . stream/imm32 # copy to EBX
|
||||
|
@ -37,8 +37,8 @@
|
|||
8b/copy 0/mod/indirect 3/rm32/EBX 3/r32/EBX # copy *EBX to EBX
|
||||
#
|
||||
b9/copy . . . . . . . a/imm32 # copy to ECX
|
||||
ba/copy . . . . . . . 1/imm32/size # copy 1 to EDX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy 4 to EAX
|
||||
ba/copy . . . . . . . 1/imm32/size # copy to EDX
|
||||
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# close(stream)
|
||||
|
@ -46,14 +46,14 @@
|
|||
bb/copy . . . . . . . stream/imm32 # copy to EBX
|
||||
8b/copy 0/mod/indirect 3/rm32/EBX 3/r32/EBX # copy *EBX to EBX
|
||||
#
|
||||
b8/copy . . . . . . . 6/imm32/close # copy 6 to EAX
|
||||
b8/copy . . . . . . . 6/imm32/close # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# stream = open(filename, O_RDONLY, 0)
|
||||
bb/copy . . . . . . . filename/imm32 # copy to EBX
|
||||
b9/copy . . . . . . . 0/imm32/rdonly # copy 0 to ECX
|
||||
b9/copy . . . . . . . 0/imm32/rdonly # copy to ECX
|
||||
ba/copy . . . . . . . 0x180/imm32/fixed-perms # copy to EDX
|
||||
b8/copy . . . . . . . 5/imm32/open # copy 5 to EAX
|
||||
b8/copy . . . . . . . 5/imm32/open # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
# save stream
|
||||
bb/copy . . . . . . . stream/imm32 # copy to EBX
|
||||
|
@ -65,8 +65,8 @@
|
|||
8b/copy 0/mod/indirect 3/rm32/EBX 3/r32/EBX # copy *EBX to EBX
|
||||
#
|
||||
b9/copy . . . . . . . b/imm32 # copy to ECX
|
||||
ba/copy . . . . . . . 1/imm32/size # copy 1 to EDX
|
||||
b8/copy . . . . . . . 3/imm32/read # copy 3 to EAX
|
||||
ba/copy . . . . . . . 1/imm32/size # copy to EDX
|
||||
b8/copy . . . . . . . 3/imm32/read # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# close(stream)
|
||||
|
@ -74,12 +74,12 @@
|
|||
bb/copy . . . . . . . stream/imm32 # copy to EBX
|
||||
8b/copy 0/mod/indirect 3/rm32/EBX 3/r32/EBX # copy *EBX to EBX
|
||||
#
|
||||
b8/copy . . . . . . . 6/imm32/close # copy 6 to EAX
|
||||
b8/copy . . . . . . . 6/imm32/close # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# unlink(filename)
|
||||
bb/copy . . . . . . . filename/imm32 # copy to EBX
|
||||
b8/copy . . . . . . . 0xa/imm32/unlink # copy 10 to EAX
|
||||
b8/copy . . . . . . . 0xa/imm32/unlink # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
# exit(b)
|
||||
|
@ -87,7 +87,7 @@
|
|||
bb/copy . . . . . . . b/imm32 # copy to EBX
|
||||
8b/copy 0/mod/indirect 3/rm32/EBX 3/r32/EBX # copy *EBX to EBX
|
||||
#
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
== data
|
||||
|
|
Binary file not shown.
|
@ -26,28 +26,28 @@
|
|||
# call
|
||||
e8/call ascii_length/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
||||
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
ascii_length: # (s)
|
||||
# initialize s (EDX)
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 2/r32/EDX 4/disp8 # copy *(ESP+4) to EDX
|
||||
# var result = 0 (EAX)
|
||||
b8/copy . . . . . . . 0/imm32 # copy 0 to EAX
|
||||
b8/copy . . . . . . . 0/imm32 # copy to EAX
|
||||
$ascii_length_loop:
|
||||
# var c = *s (ECX)
|
||||
8a/copy 0/mod/* 2/rm32/EDX . . . 1/r32/ECX . . # copy byte at *EDX to lower byte of ECX
|
||||
# if c == '\0' break
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ECX . . . . . 0/imm32 # compare ECX with 0
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ECX . . . . . 0/imm32 # compare ECX
|
||||
74/jump-if-equal $ascii_length_ret/disp8
|
||||
# ++s
|
||||
81 0/subop/add 3/mod/direct 2/rm32/EDX . . . . . 1/imm32 # add 1 to EDX
|
||||
81 0/subop/add 3/mod/direct 2/rm32/EDX . . . . . 1/imm32 # add to EDX
|
||||
# ++result
|
||||
81 0/subop/add 3/mod/direct 0/rm32/EAX . . . . . 1/imm32 # add 1 to EAX
|
||||
40/inc-EAX
|
||||
# loop
|
||||
eb/jump $ascii_length_loop/disp8
|
||||
$ascii_length_ret:
|
||||
|
|
|
@ -30,10 +30,10 @@
|
|||
# call
|
||||
e8/call ascii_difference/disp32
|
||||
# discard args
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP
|
||||
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
||||
# exit(EAX)
|
||||
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy 1 to EAX
|
||||
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
||||
cd/syscall 0x80/imm8
|
||||
|
||||
ascii_difference: # (s1, s2) : null-terminated ascii strings
|
||||
|
|
Loading…
Reference in New Issue