This commit is contained in:
Kartik Agaram 2018-09-22 10:33:41 -07:00
parent 25d381c7d4
commit 8c580ba1de
5 changed files with 27 additions and 27 deletions

View File

@ -41,9 +41,9 @@
# else EAX <- factorial(5)
$run_main:
# push arg
68/push . . . . . . . 5/imm32 # push 5
68/push 5/imm32
# EAX <- call
e8/call . . . . . . factorial/disp32
e8/call factorial/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
$main_exit:
@ -58,15 +58,15 @@ factorial:
b8/copy . . . . . . . 1/imm32 # copy 1 to EAX
# if (n <= 1) jump exit
81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . . 4/disp8 1/imm32 # compare *(ESP+4) with 1
7e/jump-if-<= . . . . . . $factorial:exit/disp8 # jump if <= to $factorial:exit
7e/jump-if-<= $factorial:exit/disp8
# EBX: n-1
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 3/r32/EBX 4/disp8 . # copy *(ESP+4) to EBX
81 5/subop/subtract 3/mod/direct 3/rm32/EBX . . . . . 1/imm32 # subtract 1 from EBX
# prepare call
55/push . . . . . . . . # push EBP
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# EAX: factorial(n-1)
53/push . . . . . . . . # push EBX
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
@ -84,9 +84,9 @@ $factorial:exit:
test_factorial:
# factorial(5)
# push arg
68/push . . . . . . . 5/imm32 # push 5
68/push 5/imm32
# call
e8/call . . . . . . factorial/disp32
e8/call factorial/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
# if EAX == 120
@ -94,9 +94,9 @@ test_factorial:
75/jump-if-unequal . . . . . . $test_factorial:else/disp8
# print('.')
# push args
68/push . . . . . . . Test_passed/imm32
68/push Test_passed/imm32
# call
e8/call . . . . . . write_stderr/disp32
e8/call write_stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
# return
@ -105,9 +105,9 @@ test_factorial:
$test_factorial:else:
# print('F')
# push args
68/push . . . . . . . Test_failed/imm32
68/push Test_failed/imm32
# call
e8/call . . . . . . write_stderr/disp32
e8/call write_stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
# end
@ -152,10 +152,10 @@ $argv_fail:
write_stderr: # s : (address array byte) -> <void>
# save registers
50/push . . . . . . . . # push EAX
51/push . . . . . . . . # push ECX
52/push . . . . . . . . # push EDX
53/push . . . . . . . . # push EBX
50/push-EAX
51/push-ECX
52/push-EDX
53/push-EBX
# write(2/stderr, (data) s+4, (size) *s)
# fd = 2 (stderr)
bb/copy . . . . . . . 2/imm32 # copy 2 to EBX
@ -169,10 +169,10 @@ write_stderr: # s : (address array byte) -> <void>
b8/copy . . . . . . . 4/imm32/write # copy 1 to EAX
cd/syscall 0x80/imm8
# restore registers
5b/pop . . . . . . . . # pop EBX
5a/pop . . . . . . . . # pop EDX
59/pop . . . . . . . . # pop ECX
58/pop . . . . . . . . # pop EAX
5b/pop-EBX
5a/pop-EDX
59/pop-ECX
58/pop-EAX
# end
c3/return

View File

@ -24,8 +24,8 @@
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . 3/r32/EBX 0xc/disp8 . # copy *(ESP+12) to EBX
# call argv_equal(s1, s2)
# push args
50/push . . . . . . . . # push EAX
53/push . . . . . . . . # push EBX
50/push-EAX
53/push-EBX
# call
e8/call argv_equal/disp32
# exit(EAX)

View File

@ -11,7 +11,7 @@
# main:
# prolog
55/push . . . . . . . . # push EBP
55/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

View File

@ -22,17 +22,17 @@
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 3/r32/EBX 8/disp8 . # copy *(ESP+8) to EBX
# call ascii_length(EBX)
# prepare call
55/push . . . . . . . . # push EBP
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# push args
53/push . . . . . . . . # push EBX
53/push-EBX
# call
e8/call ascii_length/disp32
# discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP
# clean up after call
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop . . . . . . . . # pop to EBP
5d/pop-to-EBP
# exit(EAX)
89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX

View File

@ -26,8 +26,8 @@
8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 3/r32/EBX 0xc/disp8 . # copy *(ESP+12) to EBX
# call string_equal(s1, s2)
# push args
50/push . . . . . . . . # push EAX
53/push . . . . . . . . # push EBX
50/push-EAX
53/push-EBX
# call
e8/call ascii_difference/disp32
# discard args