4467
This commit is contained in:
parent
85173b6220
commit
12adb3da8a
|
@ -55,10 +55,10 @@ cerr << " instructions\n";
|
|||
:(scenario check_missing_imm8_operand)
|
||||
% Hide_errors = true;
|
||||
== 0x1
|
||||
# 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
|
||||
cd # int ??
|
||||
# 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
|
||||
cd # int ??
|
||||
+error: 'cd' (software interrupt): missing imm8 operand
|
||||
|
||||
:(before "End One-time Setup")
|
||||
|
@ -584,10 +584,10 @@ void check_operands_f3(const line& /*unused*/) {
|
|||
:(scenario check_missing_disp16_operand)
|
||||
% Hide_errors = true;
|
||||
== 0x1
|
||||
# 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
|
||||
0f 84 # jmp if ZF to ??
|
||||
# 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
|
||||
0f 84 # jmp if ZF to ??
|
||||
+error: '0f 84' (jump disp16 bytes away if ZF is set): missing disp16 operand
|
||||
|
||||
:(before "End Globals")
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
|
||||
:(scenario pack_immediate_constants)
|
||||
== 0x1
|
||||
# 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 0x2a/imm32 # copy 42 to EBX
|
||||
# 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 0x2a/imm32 # copy 42 to EBX
|
||||
+transform: packing instruction 'bb 0x2a/imm32'
|
||||
+transform: instruction after packing: 'bb 2a 00 00 00'
|
||||
+run: copy imm32 0x0000002a to EBX
|
||||
|
@ -28,10 +28,10 @@
|
|||
|
||||
:(scenario pack_modrm_imm32)
|
||||
== 0x1
|
||||
# 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
|
||||
81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32 # add 1 to EBX
|
||||
# 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
|
||||
81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32 # add 1 to EBX
|
||||
+transform: packing instruction '81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32'
|
||||
+transform: instruction after packing: '81 c3 01 00 00 00'
|
||||
|
||||
|
@ -183,10 +183,10 @@ string to_string(const vector<word>& in) {
|
|||
|
||||
:(scenario pack_immediate_constants_hex)
|
||||
== 0x1
|
||||
# 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 0x2a/imm32 # copy 42 to EBX
|
||||
# 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 0x2a/imm32 # copy 42 to EBX
|
||||
+transform: packing instruction 'bb 0x2a/imm32'
|
||||
+transform: instruction after packing: 'bb 2a 00 00 00'
|
||||
+run: copy imm32 0x0000002a to EBX
|
||||
|
@ -194,10 +194,10 @@ string to_string(const vector<word>& in) {
|
|||
:(scenarios transform)
|
||||
:(scenario pack_silently_ignores_non_hex)
|
||||
== 0x1
|
||||
# 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 foo/imm32 # copy foo to EBX
|
||||
# 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 foo/imm32 # copy foo to EBX
|
||||
+transform: packing instruction 'bb foo/imm32'
|
||||
# no change (we're just not printing metadata to the trace)
|
||||
+transform: instruction after packing: 'bb foo'
|
||||
|
|
|
@ -4,11 +4,11 @@
|
|||
:(scenarios transform)
|
||||
:(scenario map_label)
|
||||
== 0x1
|
||||
# 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
|
||||
# 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
|
||||
loop:
|
||||
05 0x0d0c0b0a/imm32 # add to EAX
|
||||
05 0x0d0c0b0a/imm32 # add to EAX
|
||||
+transform: label 'loop' is at address 1
|
||||
|
||||
:(before "End One-time Setup")
|
||||
|
@ -138,18 +138,18 @@ string data_to_string(const line& inst) {
|
|||
|
||||
:(scenario multiple_labels_at)
|
||||
== 0x1
|
||||
# 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
|
||||
# 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
|
||||
# address 1
|
||||
loop:
|
||||
loop2:
|
||||
# address 1 (labels take up no space)
|
||||
05 0x0d0c0b0a/imm32 # add to EAX
|
||||
05 0x0d0c0b0a/imm32 # add to EAX
|
||||
# address 6
|
||||
eb loop2/disp8
|
||||
eb loop2/disp8
|
||||
# address 8
|
||||
eb loop3/disp8
|
||||
eb loop3/disp8
|
||||
# address 10
|
||||
loop3:
|
||||
+transform: label 'loop' is at address 1
|
||||
|
|
|
@ -9,12 +9,12 @@
|
|||
# 42
|
||||
|
||||
== 0x08048054 # code segment, after leaving room for ELF header
|
||||
# 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 2a/imm32 # copy 42 to EBX
|
||||
# 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 2a/imm32 # copy 42 to EBX
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
# vim:ft=subx
|
||||
|
|
|
@ -8,13 +8,13 @@
|
|||
# 2
|
||||
|
||||
== 0x08048054 # code segment, after leaving room for ELF header
|
||||
# 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
|
||||
# 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
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
# vim:ft=subx
|
||||
|
|
|
@ -8,28 +8,28 @@
|
|||
# 55
|
||||
|
||||
== 0x08048054 # code segment, after leaving room for ELF header
|
||||
# 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
|
||||
# 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
|
||||
# result: EBX = 0
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
# counter: ECX = 1
|
||||
b9/copy 1/imm32 # copy 1 to ECX
|
||||
b9/copy 1/imm32 # copy 1 to ECX
|
||||
|
||||
loop:
|
||||
# while (counter <= 10)
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ecx 0xa/imm32 # compare ECX, 10/imm
|
||||
7f/jump-if exit/disp8 # jump-if-greater exit
|
||||
81 7/subop/compare 3/mod/direct 1/rm32/ecx 0xa/imm32 # compare ECX, 10/imm
|
||||
7f/jump-if exit/disp8 # jump-if-greater exit
|
||||
# result += counter
|
||||
01/add 3/mod/direct 3/rm32/ebx 1/r32/ecx # add ECX to EBX
|
||||
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
|
||||
81 0/subop/add 3/mod/direct 1/rm32/ecx 1/imm32 # add 1 to ECX
|
||||
# loop
|
||||
eb/jump loop/disp8 # jump loop
|
||||
eb/jump loop/disp8 # jump loop
|
||||
|
||||
exit:
|
||||
# exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
# vim:ft=subx:nowrap
|
||||
|
|
|
@ -5,35 +5,35 @@
|
|||
# $ subx run ex4
|
||||
|
||||
== 0x08048074 # code segment, after leaving room for ELF header and segment headers
|
||||
# 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
|
||||
# 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
|
||||
|
||||
## read(stdin, x, 1)
|
||||
# fd = 0 (stdin)
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
# initialize x (location to write result to)
|
||||
b9/copy 0x080490a7/imm32 # copy to ECX
|
||||
b9/copy 0x080490a7/imm32 # copy to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
# read(fd, x, size)
|
||||
b8/copy 3/imm32 # copy 3 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 3/imm32 # copy 3 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
## write(stdout, x, 1)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
# initialize x (location to read from)
|
||||
b9/copy 0x080490a7/imm32 # copy to ECX
|
||||
b9/copy 0x080490a7/imm32 # copy to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
## exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
== 0x080490a7
|
||||
00 00 00 00 # space for read() to write to
|
||||
|
|
|
@ -5,41 +5,41 @@
|
|||
# $ subx run ex5
|
||||
|
||||
== 0x08048054 # code segment, after leaving room for ELF header and segment headers
|
||||
# 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
|
||||
# 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
|
||||
|
||||
## function main
|
||||
# prolog
|
||||
55/push # push EBP
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
55/push # push EBP
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
# 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 4 bytes from ESP
|
||||
|
||||
## read(stdin, x, 1)
|
||||
# fd = 0 (stdin)
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
bb/copy 0/imm32 # copy 0 to EBX
|
||||
# initialize x (location to write result to)
|
||||
89/copy 3/mod/direct 1/rm32/ECX 5/r32/EBP # copy EBP to ECX
|
||||
89/copy 3/mod/direct 1/rm32/ECX 5/r32/EBP # copy EBP to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
# read(fd, x, size)
|
||||
b8/copy 3/imm32 # copy 3 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 3/imm32 # copy 3 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
## write(stdout, x, 1)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
# initialize x (location to read from)
|
||||
89/copy 3/mod/direct 1/rm32/ECX 5/r32/EBP # copy EBP to ECX
|
||||
89/copy 3/mod/direct 1/rm32/ECX 5/r32/EBP # copy EBP to ECX
|
||||
# size = 1 character
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
ba/copy 1/imm32 # copy 1 to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
## exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
# vim:ft=subx:nowrap
|
||||
|
|
|
@ -6,25 +6,25 @@
|
|||
# Hello, world!
|
||||
|
||||
== 0x08048074 # code segment, after leaving room for ELF header and segment headers
|
||||
# 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
|
||||
# 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
|
||||
|
||||
## write(stdout, x, 1)
|
||||
# fd = 1 (stdout)
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
bb/copy 1/imm32 # copy 1 to EBX
|
||||
# initialize x (location to write result to)
|
||||
b9/copy 0x08049097/imm32 # copy to ECX
|
||||
b9/copy 0x08049097/imm32 # copy to ECX
|
||||
# initialize size
|
||||
ba/copy 0x08049093/imm32 # copy to EDX
|
||||
8b/copy 0/mod/indirect 2/rm32/edx 2/r32/edx # copy *EDX to EDX
|
||||
ba/copy 0x08049093/imm32 # copy to EDX
|
||||
8b/copy 0/mod/indirect 2/rm32/edx 2/r32/edx # copy *EDX to EDX
|
||||
# write(fd, x, size)
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 4/imm32 # copy 4 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
## exit(EBX)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
== 0x08049093
|
||||
# size of string
|
||||
|
|
|
@ -8,80 +8,80 @@
|
|||
# 120
|
||||
|
||||
== 0x08048054 # code segment, after leaving room for ELF header
|
||||
# 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
|
||||
# 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
|
||||
|
||||
# main:
|
||||
# prepare to make a call
|
||||
# 54
|
||||
55/push # push EBP
|
||||
55/push # push EBP
|
||||
# 55
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
# 57
|
||||
# factorial(5)
|
||||
68/push 5/imm32 # push 5
|
||||
68/push 5/imm32 # push 5
|
||||
# 5c
|
||||
e8/call factorial/disp32
|
||||
e8/call factorial/disp32
|
||||
# 61
|
||||
# discard arg
|
||||
5a/pop # pop into EDX
|
||||
5a/pop # pop into EDX
|
||||
# 62
|
||||
# clean up after call
|
||||
89/copy 3/mod/direct 4/rm32/ESP 5/r32/EBP # copy EBP to ESP
|
||||
89/copy 3/mod/direct 4/rm32/ESP 5/r32/EBP # copy EBP to ESP
|
||||
# 64
|
||||
5d/pop # pop to EBP
|
||||
5d/pop # pop to EBP
|
||||
|
||||
# exit(EAX)
|
||||
# 65
|
||||
89/copy 3/mod/direct 3/rm32/EBX 0/r32/EAX # copy EAX to EBX
|
||||
89/copy 3/mod/direct 3/rm32/EBX 0/r32/EAX # copy EAX to EBX
|
||||
# 67
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
# 6c
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
cd/syscall 0x80/imm8 # int 80h
|
||||
|
||||
# factorial(n)
|
||||
# 6e
|
||||
factorial:
|
||||
# initialize n
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 2/r32/EDX 4/disp8 # copy *(ESP+4) to 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
|
||||
# 72
|
||||
# initialize EAX to 1 (base case)
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
b8/copy 1/imm32 # copy 1 to EAX
|
||||
# 77
|
||||
# if (n <= 1) jump exit
|
||||
81 7/subop/compare 3/mod/direct 2/rm32/EDX 1/imm32 # compare EDX with 1
|
||||
81 7/subop/compare 3/mod/direct 2/rm32/EDX 1/imm32 # compare EDX with 1
|
||||
# 7d
|
||||
7e/jump-if factorial:exit/disp8 # jump if <= to exit
|
||||
7e/jump-if factorial:exit/disp8 # jump if <= to exit
|
||||
# 7f
|
||||
# EBX: n-1
|
||||
89/copy 3/mod/direct 3/rm32/EBX 2/r32/EDX # copy EDX to EBX
|
||||
89/copy 3/mod/direct 3/rm32/EBX 2/r32/EDX # copy EDX to EBX
|
||||
# 81
|
||||
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 1 from EBX
|
||||
# 87
|
||||
# prepare call
|
||||
55/push # push EBP
|
||||
55/push # push EBP
|
||||
# 88
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
89/copy 3/mod/direct 5/rm32/EBP 4/r32/ESP # copy ESP to EBP
|
||||
# EAX: factorial(n-1)
|
||||
# 8a
|
||||
53/push # push EBX
|
||||
53/push # push EBX
|
||||
# 8b
|
||||
e8/call factorial/disp32
|
||||
e8/call factorial/disp32
|
||||
# 90
|
||||
# discard arg
|
||||
5e/pop # pop into ESI
|
||||
5e/pop # pop into ESI
|
||||
# 91
|
||||
# clean up after call
|
||||
89/copy 3/mod/direct 4/rm32/ESP 5/r32/EBP # copy EBP to ESP
|
||||
89/copy 3/mod/direct 4/rm32/ESP 5/r32/EBP # copy EBP to ESP
|
||||
# 93
|
||||
5d/pop # pop to EBP
|
||||
5d/pop # pop to EBP
|
||||
# 94
|
||||
# refresh n
|
||||
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 2/r32/EDX 4/disp8 # copy *(ESP+4) to 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
|
||||
# 98
|
||||
# return n * factorial(n-1)
|
||||
0f af/multiply 3/mod/direct 2/rm32/EDX 0/r32/EAX # multiply EDX (n) into EAX (factorial(n-1))
|
||||
0f af/multiply 3/mod/direct 2/rm32/EDX 0/r32/EAX # multiply EDX (n) into EAX (factorial(n-1))
|
||||
# TODO: check for overflow
|
||||
# 9b
|
||||
factorial:exit:
|
||||
|
|
Loading…
Reference in New Issue