Franck Pommereau

bugs fixed in cttc.py, libc/stdio passed trivial tests

......@@ -256,11 +256,11 @@ class CodeGenVisitor (Visitor) :
node.body.accept(self)
function_str = self.curr_str
self.curr_str = old_str
frame_size = self.stack.get_max_fp()
if frame_size > 0:
frame_size = abs(self.stack.get_max_fp())
if frame_size != 0:
self.o(" set R9 %X" % frame_size,
"Allocate %s words for local+temp vars" % frame_size)
self.o(" sub R9 SP SP", "... shift SP")
self.o(" sub SP R9 SP", "... shift SP")
self.stack.save_callee_saves()
self.curr_str.write(function_str.getvalue())
self._loc(node, "(exit function)")
......@@ -538,11 +538,13 @@ class CodeGenVisitor (Visitor) :
elif node.op == "<" :
self.o(" gt %s %s %s" % (right_reg, left_reg, reg), "Perform '<'")
elif node.op == ">=" :
self.o(" gt %s %s %s" % (right_reg, left_reg, reg), "Perform '>='")
self.o(" not %s %s" % (reg, reg), "... throught 'not <'")
self.o(" inc %s" % left_reg, "Perform 'a >= b'")
self.o(" gt %s %s %s" % (left_reg, right_reg, reg),
"... that is 'a+1 > b'")
elif node.op == "<=" :
self.o(" gt %s %s %s" % (left_reg, right_reg, reg), "Perform '<='")
self.o(" not %s %s" % (reg, reg), "... throught 'not >'")
self.o(" inc %s" % right_reg, "Perform 'a <= b'")
self.o(" gt %s %s %s" % (right_reg, left_reg, reg),
"... that is 'b+1 > a'")
else :
raise Exception("unsupported comparison %r" % node.op)
......@@ -550,10 +552,12 @@ class CodeGenVisitor (Visitor) :
self._loc(node)
if node.op in cparse.Binop.ASSIGN_OPS:
self._binop_assign(node)
elif node.op in ['+','-','*']:
elif node.op in ['+','-','*','/','%']:
self._binop_arith(node)
elif node.op in ['==', '!=', '<', '>', '<=', '>=']:
self._binop_compare(node)
else :
raise Exception("unsupported operation %r" % node.op)
def vNegative(self, node):
self._loc(node)
......
all:
python3 ../cct.py --arch=ttc -p -o stdio-c.asm stdio.c
cat stdio_putc.asm > stdio.asm
tail -n +14 stdio-c.asm >> stdio.asm
# Generated by cct
# Franck Pommereau (2018)
# Adapted from Atul Varma's c.py (Spring 2004)
#
# on computer start: call main and halt
#
IRQ0:
set R9 @main
call R9
halt
#
# code from file 'stdio.c'
#
# BEGIN FUNCTION: puts() -----------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:32 (enter function)
puts:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 2 # Allocate 2 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:33
# stdio.c:35
set R9 -2 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:36
set R9 -1 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:37
# WHILE loop - begin
_L1_test:
set R7 1 # Load numeric constant 1
set R9 @_L2_done # Point towards loop exit
jz R7 R9 # ... if result is zero, jump to it
# stdio.c:38
# IF statment - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R9 @_L4_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:39
set R9 -2 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# stdio.c:40
set R9 -1 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
# IF statment - THEN clause - end
set R9 @_L3_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L4_else:
# stdio.c:41
# <string>:0
set R9 @_L2_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - ELSE clause - end
_L3_done:
# IF statment - end
set R9 @_L1_test # Point towards loop start
jmp R9 # ... jump to it
_L2_done:
# WHILE loop - end
# stdio.c:45
ldi R5 -2 # Get value of n
mov R5 R0 # Set return value
set R9 @_L0_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:32 (exit function)
_L0_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puts() -------------------------------------------------
# BEGIN FUNCTION: putu() -----------------------------------------------
#
# Function type: function(int,char)->int
# stdio.c:55 (enter function)
putu:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 6 # Allocate 6 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:56
# stdio.c:62
set R9 -4 # Get BP-relative address of DIGITS
add R9 BP R7 # Compute address of DIGITS
set R6 @_LC0 # Get addr of string literal '0123456...'
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:63
set R9 -1 # Get BP-relative address of str
add R9 BP R5 # Compute address of str
set R7 @_LC1 # Get addr of string literal '65536'
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:64
set R9 -3 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:65
set R9 -2 # Get BP-relative address of p
add R9 BP R5 # Compute address of p
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:66
# IF statment - begin
ldi R6 4 # Get value of f
set R8 78 # Use constant 120
eq R6 R8 R6 # Perform '=='
set R9 @_L7_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:67
set R9 -5 # Get BP-relative address of div
add R9 BP R6 # Compute address of div
set R8 10 # Use constant 16
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# IF statment - THEN clause - end
set R9 @_L6_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L7_else:
# stdio.c:69
set R9 -5 # Get BP-relative address of div
add R9 BP R5 # Compute address of div
set R8 A # Use constant 10
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# IF statment - ELSE clause - end
_L6_done:
# IF statment - end
# stdio.c:71
# WHILE loop - begin
_L8_test:
ldi R6 3 # Get value of u
set R9 @_L9_done # Point towards loop exit
jz R6 R9 # ... if result is zero, jump to it
# stdio.c:72
set R9 -6 # Get BP-relative address of r
add R9 BP R6 # Compute address of r
ldi R5 3 # Get value of u
ldi R7 -5 # Get value of div
mod R5 R7 R5 # Perform '%'
mov R5 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:73
ldi R7 -1 # Get value of str
ldi R6 -2 # Get value of p
add R7 R6 R5 # Load addr of array index
ldi R7 -4 # Get value of DIGITS
ldi R6 -6 # Get value of r
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
mov R4 R7 # Assignement '=': set result
st R7 R5 # ... save value
# stdio.c:74
set R9 -2 # Get BP-relative address of p
add R9 BP R7 # Compute address of p
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# stdio.c:75
set R9 3 # Get BP-relative address of u
add R9 BP R5 # Compute address of u
ldi R7 3 # Get value of u
ldi R4 -5 # Get value of div
div R7 R4 R7 # Perform '/'
mov R7 R4 # Assignement '=': set result
st R4 R5 # ... save value
set R9 @_L8_test # Point towards loop start
jmp R9 # ... jump to it
_L9_done:
# WHILE loop - end
# stdio.c:77
set R9 -2 # Get BP-relative address of p
add R9 BP R4 # Compute address of p
set R8 1 # Use constant 1
ld R4 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R4 # ... save value
# stdio.c:78
# WHILE loop - begin
_L10_test:
ldi R5 -2 # Get value of p
set R8 0 # Use constant 0
inc R5 # Perform 'a >= b'
gt R5 R8 R5 # ... that is 'a+1 > b'
set R9 @_L11_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:79
set R9 -3 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 -1 # Get value of str
ldi R7 -2 # Get value of p
add R4 R7 R6 # Load addr of array index
ld R6 R6 # Load array value
push R6 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R5 # ... save value
# stdio.c:80
set R9 -2 # Get BP-relative address of p
add R9 BP R6 # Compute address of p
set R8 1 # Use constant 1
ld R6 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R6 # ... save value
set R9 @_L10_test # Point towards loop start
jmp R9 # ... jump to it
_L11_done:
# WHILE loop - end
# stdio.c:82
ldi R5 -3 # Get value of n
mov R5 R0 # Set return value
set R9 @_L5_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:55 (exit function)
_L5_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: putu() -------------------------------------------------
# BEGIN FUNCTION: puti() -----------------------------------------------
#
# Function type: function(int)->int
# stdio.c:85 (enter function)
puti:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 1 # Allocate 1 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
# stdio.c:86
# stdio.c:87
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:88
# IF statment - begin
ldi R6 3 # Get value of i
set R8 7FFF # Use constant 32767
gt R6 R8 R6 # Perform '>'
set R9 @_L14_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:89
set R9 -1 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 2D # Use constant 45
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R6 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:90
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
neg R6 R6 # Perform unary negation
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ld R7 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R7 # ... save value
# stdio.c:91
ldi R6 -1 # Get value of n
mov R6 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - THEN clause - end
set R9 @_L13_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L14_else:
# stdio.c:93
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
mov R0 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - ELSE clause - end
_L13_done:
# IF statment - end
# stdio.c:85 (exit function)
_L12_function_end:
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puti() -------------------------------------------------
# BEGIN FUNCTION: printf() ---------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:97 (enter function)
printf:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 9 # Allocate 9 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:98
# stdio.c:103
set R9 -1 # Get BP-relative address of iargs
add R9 BP R7 # Compute address of iargs
set R9 3 # Get BP-relative address of str
add R9 BP R6 # Compute address of str
# (Address-of operator '&' used here)
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:104
set R9 -3 # Get BP-relative address of cargs
add R9 BP R5 # Compute address of cargs
set R9 3 # Get BP-relative address of str
add R9 BP R7 # Compute address of str
# (Address-of operator '&' used here)
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:105
set R9 -5 # Get BP-relative address of a
add R9 BP R6 # Compute address of a
set R8 1 # Use constant 1
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:106
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:107
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:108
# WHILE loop - begin
_L16_test:
set R5 1 # Load numeric constant 1
set R9 @_L17_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:109
# IF statment - begin
ldi R5 3 # Get value of str
ldi R6 -2 # Get value of i
add R5 R6 R7 # Load addr of array index
ld R7 R7 # Load array value
set R8 0 # Use constant 0
eq R7 R8 R7 # Perform '=='
set R9 @_L19_else # Point towards else clause
jz R7 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# <string>:0
set R9 @_L17_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - THEN clause - end
set R9 @_L18_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L19_else:
# stdio.c:111
# IF statment - begin
ldi R7 3 # Get value of str
ldi R5 -2 # Get value of i
add R7 R5 R6 # Load addr of array index
ld R6 R6 # Load array value
set R8 25 # Use constant 37
eq R6 R8 R6 # Perform '=='
set R9 @_L21_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:112
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 1 # Use constant 1
ld R6 R7 # Assignement '+=': load initial left value
add R7 R8 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:113
# IF statment - begin
ldi R7 3 # Get value of str
ldi R6 -2 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R8 69 # Use constant 105
eq R5 R8 R5 # Perform '=='
set R9 @_L23_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:114
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to puti() - begin
ldi R7 -1 # Get value of iargs
ldi R6 -5 # Get value of a
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @puti # Point towards function puti()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puti() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L22_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L23_else:
# stdio.c:115
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 75 # Use constant 117
eq R0 R8 R0 # Perform '=='
set R9 @_L25_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:116
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -6 # Save caller-save register to temp
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -6 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L24_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L25_else:
# stdio.c:117
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 73 # Use constant 115
eq R0 R8 R0 # Perform '=='
set R9 @_L27_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:118
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to puts() - begin
sti R0 -7 # Save caller-save register to temp
ldi R0 -3 # Get value of cargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @puts # Point towards function puts()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puts() - end
ldi R5 -7 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L26_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L27_else:
# stdio.c:119
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 78 # Use constant 120
eq R0 R8 R0 # Perform '=='
set R9 @_L29_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:120
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -8 # Save caller-save register to temp
set R8 78 # Use constant 120
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -8 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L28_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L29_else:
# stdio.c:121
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 25 # Use constant 37
eq R0 R8 R0 # Perform '=='
set R9 @_L31_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:122
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putc() - begin
sti R0 -9 # Save caller-save register to temp
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ldi R4 -9 # Stack machine: copy temp to register
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:123
set R9 -2 # Get BP-relative address of i
add R9 BP R5 # Compute address of i
set R8 1 # Use constant 1
ld R5 R4 # Assignement '+=': load initial left value
add R4 R8 R4 # ... add right value
st R4 R5 # ... save value
# <string>:0
set R9 @_L16_test # Loop: continue statement
jmp R9 # ... jump to loop start
# IF statment - THEN clause - end
set R9 @_L30_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L31_else:
# stdio.c:126
set R9 -4 # Get BP-relative address of n
add R9 BP R4 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:127
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 3 # Get value of str
ldi R0 -2 # Get value of i
add R4 R0 R7 # Load addr of array index
ld R7 R7 # Load array value
push R7 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R5 # ... save value
# IF statment - ELSE clause - end
_L30_done:
# IF statment - end
# IF statment - ELSE clause - end
_L28_done:
# IF statment - end
# IF statment - ELSE clause - end
_L26_done:
# IF statment - end
# IF statment - ELSE clause - end
_L24_done:
# IF statment - end
# IF statment - ELSE clause - end
_L22_done:
# IF statment - end
# stdio.c:129
set R9 -5 # Get BP-relative address of a
add R9 BP R7 # Compute address of a
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# IF statment - THEN clause - end
set R9 @_L20_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L21_else:
# stdio.c:131
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of str
ldi R0 -2 # Get value of i
add R7 R0 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - ELSE clause - end
_L20_done:
# IF statment - end
# IF statment - ELSE clause - end
_L18_done:
# IF statment - end
# stdio.c:133
set R9 -2 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
set R9 @_L16_test # Point towards loop start
jmp R9 # ... jump to it
_L17_done:
# WHILE loop - end
# stdio.c:135
ldi R5 -4 # Get value of n
mov R5 R0 # Set return value
set R9 @_L15_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:97 (exit function)
_L15_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: printf() -----------------------------------------------
#
# string literals from file 'stdio.c'
#
_LC0:
str "0123456789ABCDEF\0"
_LC1:
str "65536\0"
#
# extern int putc(char c)
#
putc:
set R8 2 # Point R8 towards argument c
add R8 SP R8 # ... located as SP+2
ld R8 R8 # Load argument c
set R9 1012 # Put command for screen
bus R9 R0 R8 # Write char to screen
set R0 1 # Set return value
ret
#
# code from file 'stdio.c'
#
# BEGIN FUNCTION: puts() -----------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:32 (enter function)
puts:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 2 # Allocate 2 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:33
# stdio.c:35
set R9 -2 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:36
set R9 -1 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:37
# WHILE loop - begin
_L1_test:
set R7 1 # Load numeric constant 1
set R9 @_L2_done # Point towards loop exit
jz R7 R9 # ... if result is zero, jump to it
# stdio.c:38
# IF statment - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R9 @_L4_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:39
set R9 -2 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# stdio.c:40
set R9 -1 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
# IF statment - THEN clause - end
set R9 @_L3_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L4_else:
# stdio.c:41
# <string>:0
set R9 @_L2_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - ELSE clause - end
_L3_done:
# IF statment - end
set R9 @_L1_test # Point towards loop start
jmp R9 # ... jump to it
_L2_done:
# WHILE loop - end
# stdio.c:45
ldi R5 -2 # Get value of n
mov R5 R0 # Set return value
set R9 @_L0_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:32 (exit function)
_L0_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puts() -------------------------------------------------
# BEGIN FUNCTION: putu() -----------------------------------------------
#
# Function type: function(int,char)->int
# stdio.c:55 (enter function)
putu:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 6 # Allocate 6 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:56
# stdio.c:62
set R9 -4 # Get BP-relative address of DIGITS
add R9 BP R7 # Compute address of DIGITS
set R6 @_LC0 # Get addr of string literal '0123456...'
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:63
set R9 -1 # Get BP-relative address of str
add R9 BP R5 # Compute address of str
set R7 @_LC1 # Get addr of string literal '65536'
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:64
set R9 -3 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:65
set R9 -2 # Get BP-relative address of p
add R9 BP R5 # Compute address of p
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:66
# IF statment - begin
ldi R6 4 # Get value of f
set R8 78 # Use constant 120
eq R6 R8 R6 # Perform '=='
set R9 @_L7_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:67
set R9 -5 # Get BP-relative address of div
add R9 BP R6 # Compute address of div
set R8 10 # Use constant 16
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# IF statment - THEN clause - end
set R9 @_L6_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L7_else:
# stdio.c:69
set R9 -5 # Get BP-relative address of div
add R9 BP R5 # Compute address of div
set R8 A # Use constant 10
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# IF statment - ELSE clause - end
_L6_done:
# IF statment - end
# stdio.c:71
# WHILE loop - begin
_L8_test:
ldi R6 3 # Get value of u
set R9 @_L9_done # Point towards loop exit
jz R6 R9 # ... if result is zero, jump to it
# stdio.c:72
set R9 -6 # Get BP-relative address of r
add R9 BP R6 # Compute address of r
ldi R5 3 # Get value of u
ldi R7 -5 # Get value of div
mod R5 R7 R5 # Perform '%'
mov R5 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:73
ldi R7 -1 # Get value of str
ldi R6 -2 # Get value of p
add R7 R6 R5 # Load addr of array index
ldi R7 -4 # Get value of DIGITS
ldi R6 -6 # Get value of r
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
mov R4 R7 # Assignement '=': set result
st R7 R5 # ... save value
# stdio.c:74
set R9 -2 # Get BP-relative address of p
add R9 BP R7 # Compute address of p
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# stdio.c:75
set R9 3 # Get BP-relative address of u
add R9 BP R5 # Compute address of u
ldi R7 3 # Get value of u
ldi R4 -5 # Get value of div
div R7 R4 R7 # Perform '/'
mov R7 R4 # Assignement '=': set result
st R4 R5 # ... save value
set R9 @_L8_test # Point towards loop start
jmp R9 # ... jump to it
_L9_done:
# WHILE loop - end
# stdio.c:77
set R9 -2 # Get BP-relative address of p
add R9 BP R4 # Compute address of p
set R8 1 # Use constant 1
ld R4 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R4 # ... save value
# stdio.c:78
# WHILE loop - begin
_L10_test:
ldi R5 -2 # Get value of p
set R8 0 # Use constant 0
inc R5 # Perform 'a >= b'
gt R5 R8 R5 # ... that is 'a+1 > b'
set R9 @_L11_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:79
set R9 -3 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 -1 # Get value of str
ldi R7 -2 # Get value of p
add R4 R7 R6 # Load addr of array index
ld R6 R6 # Load array value
push R6 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R5 # ... save value
# stdio.c:80
set R9 -2 # Get BP-relative address of p
add R9 BP R6 # Compute address of p
set R8 1 # Use constant 1
ld R6 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R6 # ... save value
set R9 @_L10_test # Point towards loop start
jmp R9 # ... jump to it
_L11_done:
# WHILE loop - end
# stdio.c:82
ldi R5 -3 # Get value of n
mov R5 R0 # Set return value
set R9 @_L5_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:55 (exit function)
_L5_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: putu() -------------------------------------------------
# BEGIN FUNCTION: puti() -----------------------------------------------
#
# Function type: function(int)->int
# stdio.c:85 (enter function)
puti:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 1 # Allocate 1 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
# stdio.c:86
# stdio.c:87
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:88
# IF statment - begin
ldi R6 3 # Get value of i
set R8 7FFF # Use constant 32767
gt R6 R8 R6 # Perform '>'
set R9 @_L14_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:89
set R9 -1 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 2D # Use constant 45
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R6 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:90
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
neg R6 R6 # Perform unary negation
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ld R7 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R7 # ... save value
# stdio.c:91
ldi R6 -1 # Get value of n
mov R6 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - THEN clause - end
set R9 @_L13_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L14_else:
# stdio.c:93
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
mov R0 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - ELSE clause - end
_L13_done:
# IF statment - end
# stdio.c:85 (exit function)
_L12_function_end:
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puti() -------------------------------------------------
# BEGIN FUNCTION: printf() ---------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:97 (enter function)
printf:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 9 # Allocate 9 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:98
# stdio.c:103
set R9 -1 # Get BP-relative address of iargs
add R9 BP R7 # Compute address of iargs
set R9 3 # Get BP-relative address of str
add R9 BP R6 # Compute address of str
# (Address-of operator '&' used here)
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:104
set R9 -3 # Get BP-relative address of cargs
add R9 BP R5 # Compute address of cargs
set R9 3 # Get BP-relative address of str
add R9 BP R7 # Compute address of str
# (Address-of operator '&' used here)
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:105
set R9 -5 # Get BP-relative address of a
add R9 BP R6 # Compute address of a
set R8 1 # Use constant 1
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:106
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:107
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:108
# WHILE loop - begin
_L16_test:
set R5 1 # Load numeric constant 1
set R9 @_L17_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:109
# IF statment - begin
ldi R5 3 # Get value of str
ldi R6 -2 # Get value of i
add R5 R6 R7 # Load addr of array index
ld R7 R7 # Load array value
set R8 0 # Use constant 0
eq R7 R8 R7 # Perform '=='
set R9 @_L19_else # Point towards else clause
jz R7 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# <string>:0
set R9 @_L17_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - THEN clause - end
set R9 @_L18_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L19_else:
# stdio.c:111
# IF statment - begin
ldi R7 3 # Get value of str
ldi R5 -2 # Get value of i
add R7 R5 R6 # Load addr of array index
ld R6 R6 # Load array value
set R8 25 # Use constant 37
eq R6 R8 R6 # Perform '=='
set R9 @_L21_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:112
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 1 # Use constant 1
ld R6 R7 # Assignement '+=': load initial left value
add R7 R8 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:113
# IF statment - begin
ldi R7 3 # Get value of str
ldi R6 -2 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R8 69 # Use constant 105
eq R5 R8 R5 # Perform '=='
set R9 @_L23_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:114
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to puti() - begin
ldi R7 -1 # Get value of iargs
ldi R6 -5 # Get value of a
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @puti # Point towards function puti()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puti() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L22_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L23_else:
# stdio.c:115
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 75 # Use constant 117
eq R0 R8 R0 # Perform '=='
set R9 @_L25_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:116
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -6 # Save caller-save register to temp
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -6 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L24_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L25_else:
# stdio.c:117
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 73 # Use constant 115
eq R0 R8 R0 # Perform '=='
set R9 @_L27_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:118
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to puts() - begin
sti R0 -7 # Save caller-save register to temp
ldi R0 -3 # Get value of cargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @puts # Point towards function puts()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puts() - end
ldi R5 -7 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L26_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L27_else:
# stdio.c:119
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 78 # Use constant 120
eq R0 R8 R0 # Perform '=='
set R9 @_L29_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:120
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -8 # Save caller-save register to temp
set R8 78 # Use constant 120
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -8 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L28_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L29_else:
# stdio.c:121
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 25 # Use constant 37
eq R0 R8 R0 # Perform '=='
set R9 @_L31_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:122
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putc() - begin
sti R0 -9 # Save caller-save register to temp
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ldi R4 -9 # Stack machine: copy temp to register
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:123
set R9 -2 # Get BP-relative address of i
add R9 BP R5 # Compute address of i
set R8 1 # Use constant 1
ld R5 R4 # Assignement '+=': load initial left value
add R4 R8 R4 # ... add right value
st R4 R5 # ... save value
# <string>:0
set R9 @_L16_test # Loop: continue statement
jmp R9 # ... jump to loop start
# IF statment - THEN clause - end
set R9 @_L30_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L31_else:
# stdio.c:126
set R9 -4 # Get BP-relative address of n
add R9 BP R4 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:127
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 3 # Get value of str
ldi R0 -2 # Get value of i
add R4 R0 R7 # Load addr of array index
ld R7 R7 # Load array value
push R7 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R5 # ... save value
# IF statment - ELSE clause - end
_L30_done:
# IF statment - end
# IF statment - ELSE clause - end
_L28_done:
# IF statment - end
# IF statment - ELSE clause - end
_L26_done:
# IF statment - end
# IF statment - ELSE clause - end
_L24_done:
# IF statment - end
# IF statment - ELSE clause - end
_L22_done:
# IF statment - end
# stdio.c:129
set R9 -5 # Get BP-relative address of a
add R9 BP R7 # Compute address of a
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# IF statment - THEN clause - end
set R9 @_L20_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L21_else:
# stdio.c:131
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of str
ldi R0 -2 # Get value of i
add R7 R0 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - ELSE clause - end
_L20_done:
# IF statment - end
# IF statment - ELSE clause - end
_L18_done:
# IF statment - end
# stdio.c:133
set R9 -2 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
set R9 @_L16_test # Point towards loop start
jmp R9 # ... jump to it
_L17_done:
# WHILE loop - end
# stdio.c:135
ldi R5 -4 # Get value of n
mov R5 R0 # Set return value
set R9 @_L15_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:97 (exit function)
_L15_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: printf() -----------------------------------------------
#
# string literals from file 'stdio.c'
#
_LC0:
str "0123456789ABCDEF\0"
_LC1:
str "65536\0"
......@@ -84,8 +84,6 @@ int putu(int u, char f) {
int puti(int i) {
int n;
int s;
int r;
n = 0;
if (UINT(i) > 32767) {
n += putc(MINUS);
......@@ -137,16 +135,16 @@ int printf(char *str, ...) {
return n;
}
int main() {
puts("hello world\n");
puti(42);
puts(" => 42\n");
puti(-42);
puts(" => -42\n");
putu(42, XCHAR);
puts(" => 2A\n");
putu(42, UCHAR);
puts(" => 42\n");
printf("hello %s I'm %u (%x)\n", "stdio", 42, 42);
return 0;
}
/* int main() { */
/* puts("hello world\n"); */
/* putu(42, XCHAR); */
/* puts(" => 2A\n"); */
/* putu(42, UCHAR); */
/* puts(" => 42\n"); */
/* puti(42); */
/* puts(" => 42\n"); */
/* puti(-42); */
/* puts(" => -42\n"); */
/* printf("hello %s I'm %u (%x)\n", "stdio", 42, 42); */
/* return 0; */
/* } */
......
#
# extern int putc(char c)
#
putc:
set R8 3 # Point R8 towards argument c
add R8 SP R8 # ... located as SP+3
set R8 2 # Point R8 towards argument c
add R8 SP R8 # ... located as SP+2
ld R8 R8 # Load argument c
set R9 1012 # Put command for screen
bus R9 R8 R8 # Write char to screen
bus R9 R0 R8 # Write char to screen
set R0 1 # Set return value
ret
......