We design an algorithm for solving the given problem using high-level pseudocode containing constructs similar to a high-level language like python or java. For example,
input a
input b
if a < b then
output a
else
output bWe then translate the pseudocode into SLIM assembly language.
read 0
read 1
li 3 7
slt 2 0 1
jeqz 2 3
write 0
halt
write 1
halt Expressions and assignment statements are implemented using SLIM’s arithmetic, comparison, and data movement commands. The tricky part is in dividing the high-level expressions into a much larger number of SLIM instructions. E.g., a python statement
a = b*b + c*cbecomes
mul b2 b b
mul c2 c c
add a b2 c2
in SLIM assembly instructions, where b, b2, c, c2, and a are register numbers with register b containing the variable b and register c containing the variable c.
A python if statement like
if a < b:
<st1>
<st2>
<st3>gets translated into
li after afterL
slt testResult a b
jeqz testResult after
<st1>
<st2>
afterL:
<st3>A python if..else statement like
if a < b:
<st1>
<st2>
else:
<st3>
<st4>
<st5>gets translated into
li else elseL
li after afterL
slt testResult a b
jeqz testResult else
<st1>
<st2>
j after
elseL:
<st3>
<st4>
afterL:
<st5>A python while statement like
while a < b:
<st1>
<st2>
<st3>gets translated into
li loop loopL
li end endL
loopL:
slt testResult a b
jeqz testResult end
<st1>
<st2>
j loop
endL:
<st3>A python for statement like
for x in range(a, b):
<st1>
<st2>gets translated into the while statement
x = a
while x < b:
<st1>
<st2>
x += 1and then we can translate the while statement like in the previous slide.