Difference between revisions of "CSC103 Assembly Language Exercises and Solutions"
(→JMZ) |
(→JMZ) |
||
Line 186: | Line 186: | ||
: write a loop that stores 55 in the memory locations between 15 and 30 | : write a loop that stores 55 in the memory locations between 15 and 30 | ||
<code><pre> | <code><pre> | ||
+ | ; Exercise 8 | ||
+ | ; D. Thiebaut | ||
+ | ; A program that loops 10 times | ||
+ | ; | ||
+ | start: | ||
+ | loop: lod-c 55 ; store 55 indirecty where | ||
+ | sto-i index ; index is pointing | ||
+ | lod index ; get index | ||
+ | add-c 1 ; increment its value | ||
+ | sto index ; store value back in index | ||
+ | lod counter ; load counter | ||
+ | sub-c 1 ; decrement it | ||
+ | sto counter ; store new value in counter | ||
+ | |||
+ | jmz done ; if value in acc is 0, jump to done | ||
+ | jmp loop ; otherwise, go back to loop label | ||
+ | done: hlt ; we're done. Stop! | ||
+ | |||
+ | @13 | ||
+ | counter: 16 ; between 15 and 30, there are 16 words of memory | ||
+ | index: 15 | ||
+ | var1: data | ||
</pre></code> | </pre></code> |
Revision as of 12:07, 20 February 2011
LOD-C 1
- Exercise 1
- initialize the contents of several variables to 0
; Exercise 1
; D. Thiebaut
; Initialize the contents of several variables
; to 55
start:
lod-c 55 ; put 0 in acc
sto var1 ; store acc in var1
sto var2 ; store acc in var2
sto var3 ; store acc in var3
hlt
@10 ; the data section starts here
var1: data
var2: data
var3: data
LOD 10/STO 10
- Exercise 2
- increment a counter
; Exercise 2
; D. Thiebaut
; A program that sets a counter to 0
; and then increments it
;
start: lod-c 0 ; first initialize the counter to 0
sto counter
; increment counter
lod counter ; get counter in acc
add-c 1 ; add 1 to acc
sto counter ; store acc in counter
hlt
@10
counter: data
- Exercise 3
- compute sum of 3 variables
; Exercise 3
; D. Thiebaut
; Compute the sum of 3 variables
; and stores it into a 4th variable called
; sum.
start: lod var1 ; get var1 in acc
add var2 ; add var2 to acc
add var3 ; add var3 to acc
sto sum ; store acc into sum
hlt
@10
var1: 12
var2: 5
var3: 3
sum: 0
LOD-I 10/STO-I 10
'Exercise 4:
- create an index and use it to sum up the same 3 variables
; Exercise 4
; D. Thiebaut
; Compute the sum of 3 variables using an index
; and stores it into a 4th variable called
; sum.
start: lod-i index ; get var1 indirectly in acc
sto sum ; set sum to var1
lod index ; get index in acc
add-c 1 ; increment acc
sto index
lod-i index ; get var2 indirectly in acc
add sum ; add sum to acc
; acc now contains var1 + var2
sto sum ; store acc in sum
lod index ; increment index
add-c 1
sto index
lod-i index ; get var3 indirectly in acc
add sum ; add var3 to sum (which contains var1+var2)
sto sum ; store acc back in sum
; sum now contains var1+var2+var3
hlt
@15
index: var1 ; index points to var1
var1: 12
var2: 5
var3: 3
sum: 0
JMP instruction and labels
- Exercise 5
- create an infinite loop (incrementing a variable, for example)
; Exercise 5
; D. Thiebaut
; an infinite loop
start: lod index ; increment index
add-c 1
sto index
jmp start
hlt
@15
index: data
- Exercise 6
- create an infinite loop that stores 55 in all the memory words the memory starting at 10
; Exercise 6
; D. Thiebaut
; an infinite loop
start: lod-c 10 ; store 10 in index
sto index
loop: lod-c 55 ; acc set to 55
sto-i index ; store 55 where index is pointing
lod index ; increment index
add-c 1
sto index
jmp loop ; loop forever
hlt
@9
index: data
- Exercise 7
- why does the program of Exercise 6 stop or misbehave?
The memory contains 1024 words of memory. So, each address can be expressed with 10 bits. Address 0 decimal is written as 0000000000 in binary. Address 1023 is written as 1111111111 in binary. So only 10 bits are used by the memory to figure out what address the processor wants to access. When the processor sends Address 1024, in binary it is written with 11 bits: 10000000000, but the memory will only look at the lower 10 bits, which are 0000000000, or memory word 0.
So the program, once it has reached the memory word at 1023 and stored 55 there, continues on with the words at address 0, 1, 2... Essentially the program overwrites itself from memory.
JMZ
Exercise 8
- write a loop that loops 10 times (use a counter)
; Exercise 8
; D. Thiebaut
; A program that loops 10 times
;
start: lod-c 10 ; store 10 in counter
sto counter
loop: lod counter ; decrement counter
sub-c 1
sto counter ; store new value in counter
jmz done ; if value in acc is 0, jump to done
jmp loop ; otherwise, go back to loop label
done: hlt ; we're done. Stop!
@10
counter: data
Exercise 9
- write a loop that stores 55 in the memory locations between 15 and 30
; Exercise 8
; D. Thiebaut
; A program that loops 10 times
;
start:
loop: lod-c 55 ; store 55 indirecty where
sto-i index ; index is pointing
lod index ; get index
add-c 1 ; increment its value
sto index ; store value back in index
lod counter ; load counter
sub-c 1 ; decrement it
sto counter ; store new value in counter
jmz done ; if value in acc is 0, jump to done
jmp loop ; otherwise, go back to loop label
done: hlt ; we're done. Stop!
@13
counter: 16 ; between 15 and 30, there are 16 words of memory
index: 15
var1: data