CSC103 2011 Assembly Language Examples
--D. Thiebaut 11:37, 15 February 2011 (EST)
Contents
Program 1
A program that computes the sum of all the integers between 1 and 5 and stores it into a variable called sum.
start: lod counter
add sum
sto sum
lod counter
sub-c 1
sto counter
jmz done
jmp start
done: hlt
counter: 5
sum: 0
Program 2
; program to display a bit pattern in graphics memory
start: lod pattern ;get the pattern
sto-i index ;store it in the image, at a location
lod index ;get the image index
add-c 1 ;make it point to next word in mem
sto index ;store it back
lod count ;get the counter
sub-c 1 ;subtract one
sto count ;store back
jmz done ;if counter is 0, we're done
lod pattern ;if not, we check the pattern
add pattern ;shift it to the left by 1 bit
sto pattern ;store it back
jmz resetPattern ;if pattern is 0, reset it
jmp start ;loop back
resetPattern: ;reset pattern to 1
lod-c 1
sto pattern
jmp start
done: hlt ;we're done!
pattern: 1 ;the pattern to store in graphics mem
count: 1000 ;how many words we fill up
index: image ;pointer to the image words
image: data ;the image starts here
Program #3
- Add 4 numbers together. No loops.
start: lod data1
add data2
add data3
add data4
sto sum
hlt
@14
data1: 1
data2: 2
data3: 3
data4: 4
sum: data
Program 4
- Add all the numbers between 1 and 10 in a loop
start: lod count
add sum
sto sum
lod count
sub-c 1
sto count
jmz done
jmp start
done: hlt
@14
count: 10
sum: data