CS-A1120 Programming 2
Department of Computer Science
Aalto University
(Based on material by Petteri Kaski and Tommi Junttila)
Always, but especially in this round!
Every line of it!
armlet
assembly languageBinary representation of data
Combinatorial logic
Combinatorial logic
Combinatorial logic
Sequential logic, for state
ALU = Arithmetic Logic Unit
$0
… $7
are registers, internal stateimmed_in
allows data inputoperation
encodes which operation to performL
, A
, B
are desination (L
) and source registers (A
, B
)operation
, L
, A
, B
(total 16 bits = one word), and if needed immed_in
(16 bits)Boooring (and slow) - let's get the machine to automatically read the next instruction
mem_addr
size \(n\) can hold \(2^n\) words of data
(Using sequential logic)
(Armlet is an example of a von Neumann architecture)
Two new ('special') registers: Program Counter and Processor Status
(And in which order)
(How does the processor know when to stop executing?)
Armlet Machine code(s) | Armlet assembly |
0111000010000111 |
sub $2, $0, $7 |
0000001011011100 0011000000111001 |
ior $3, $1, 12345 |
armlet
):var
's (the registers) and one huge array (the memory)Scala
var t = 10
var i = 1
var s = 0
while t != 0 do
s = s + i
i = i + 1
t = t - 1
// Now the sum is in 's'
Armlet
mov $0, 10 # $0 = 10
mov $1, 1 # $1 = 1
mov $2, 0 # $2 = 0
@loop: # This is a label
cmp $0, 0 # $0 == 0 ? (PS)
beq >done # if PS is eq
add $2, $2, $1 # $2 = $2 + $1
add $1, $1, 1 # $1 = $1 + 1
sub $0, $0, 1 # $0 = $0 - 1
jmp >loop # Jump back
@done: # Another label
hlt # Stop, sum in $2
kwd $L, $A, $B
kwd $L, $A, I
kwd $L, $A
kwd $A
kwd
kwd
is a three letter abbreviation for the instruction$L
is the register where the destination should be stored (e.g. $3
)$A
and $B
are input registersI
is immediate data (i.e. a constant such as 235
or 0xF3
)#
add $4, $0, $3 # Add values in $0 and $3 and store result in $4
(From the armlet quick reference)
@done:
on a line will have the assembler associate that memory address with the label 'done' jmp >target # jump to label @target
# ... some code here ...
@target:
# ... some further code here ...
(Note that you can jump both forwards and backwards in the code)
cmp $7,0 # compare value in $7 to 0
beq >done # jump to label @done if left == right
# ... some code here ...
@done:
# ... and more code here ...
cmp
updates the Processor Status registercmp
callcmp
and the branch!
(From the armlet quick reference)
$0
, $1
, $2
when execution halts? mov $0, 18
mov $1, 15
cmp $0, $1
blt >labA
mov $2, $0
jmp >labB
@labA:
mov $2, $1
@labB:
lsl $2, $2, 1
hlt
instruction | what it does |
---|---|
blt X |
Jump to X if left < right (in prev. cmp ) |
cmp $A, $B |
Compare A and B |
hlt |
Hale execution |
jmp X |
Jump (go to) X |
lsl $L, $A, $B |
L = A << B (left shift) |
mov $L, I |
L = A |
In the exercise material, run the program launchTicker.scala
in the armlet
sub-project.
Or, open a console: sbt armlet/console
and do
import armlet._
new Ticker()
loa
and sto
can be used to move words between memory and registersloa x,y
is x = mem(y)
, and sto x,y
is mem(y) = x
.)
(In armlet
PC always starts at 0, so data should always come after program.)
# Computes (to $0) the sum of the data and then halts.
# Let us first set things up ...
mov $0, 0 # the sum starts with 0
mov $2, >length_of_my_data # set up memory address where to get length
loa $2, $2 # load the length from memory to $2
mov $1, >my_data # set up memory address where to get the data
# Now let us loop through the data and accumulate the sum ...
@sum_loop:
cmp $2, 0 # compare length with 0
beq >done # ... branch to label @done if $2 == 0
loa $3, $1 # load a data item from memory
add $0, $0, $3 # accumulate the sum in $0
add $1, $1, 1 # advance to next data item
sub $2, $2, 1 # decrement length by one
jmp >sum_loop # continue the summation
# ... until we are done
@done:
hlt # the processor stops here
# Our data follows the program code in the binary ...
@length_of_my_data:
%data 20
@my_data:
%data 296, 573, 291, 415, 825, 674, 426, 632, 793, 701, 884, 1, 989, 912, 254, 869, 462, 296, 767, 220
(%data
is a directive - an assembly helper tool; %data 20
will translate the literal 20 to binary and write it as data to memory)
More examples in the course notes
But, it is hard work as a programmer…
(Comment your code!)
Solved?
A programmable machine can, with right programming, simulate another programmable machine
How can two program run simultaneously? How can a program wait for input? Print output? How are high level programs translated into machine code?