Assembly Language Worksheet 1
Assembly Language Worksheet 1
Use the instructions in Table 1 on the next page for all questions on this worksheet
Task 1
1. A positive number is held in memory location 201. Write assembly code instructions which
put the negative of the number in location 202. ADD [4]
2. Write assembly code instructions to take three numbers held in locations 201, 202 and 203
and store them back in the same locations in reverse order. [4]
ADD
3. Three numbers are held in locations 201, 202 and 203. Write assembly code instructions to
store the maximum of the three numbers in location 300. [5]
4. Assume that numbers can be input by a user into Register 0 using the instruction
INP R0
(a) Ten numbers are input by the user. Write assembly code instructions to add the ten
numbers as they are input and store the result in location 300. [5]
(b) Ten integers are input by the user. Write assembly code instructions to count the
number of integers that are greater than or equal to 30. The result should be stored in
location 300. [6]
Task 2
1. Write assembly code instructions to find whether a number held in location 200 is odd or
even. [5]
(a) Write assembly code instructions to initialise the switches to zero, and then turn on
switches 1,3,5 and 7. [2]
(b) Assume you do not know the state of the switches in R0. Write an assembly code
instruction to turn off any switches that are on, and vice versa. [1]
3. What will be the effect of performing an XOR operation on an operand with itself [1]
Give an example. [1]
Task 3
1. Assume R0 contains an 8-bit positive integer. Using logical shifts, compare and branch
operations, write assembly code statements to branch to LABEL1 if the integer represents
an even number, otherwise continue to the next statement. [5]
Test your program by tracing the contents of R0 and any other registers used. [3]
Can you think of another way of testing whether the number is even? [2]
1
Worksheet 1 Assembly language
Unit 5 Computer organisation
Table 1: Assembly language instructions
<operand> can be interpreted in two different ways, depending upon whether the first symbol
is a # or an R:
# – Use the decimal value specified after the #, e.g. #25 means use the decimal value 25.
Rm – Use the value stored in register m, e.g. R6 means use the value stored in register 6.
The available general purpose registers that the programmer can use are numbered 0 to 7.
2
Worksheet 1 Assembly language
Unit 5 Computer organisation
The assembly code instruction formats in Table 1 are used in the questions in this
homework.
(b) Explain what the instruction SUB R1, 102 does the first time round the loop starting
at LABEL1.
[1]
2. (a) Write assembly code instructions to compare the contents of register 1 and register 2
and branch to LABEL1 if they are equal. [2]
(b) Explain with the aid of examples the difference between immediate and direct
addressing. [2]
3
Worksheet 1 Assembly language
Unit 5 Computer organisation
3. Write the assembly code instructions which are equivalent to the following high-level code.
Comment each line of code.
x 0
WHILE x < 1000
x = x + 1
ENDWHILE [6]
4. In a particular computer, characters are represented by 8-bit patterns. The codes for
uppercase letters are from 0100 0001 for A to 0101 1010 for Z. The codes for lowercase
letters are from 0110 0001 for a to 0111 1010 for z. Give an appropriate mask and logical
operation which will:
(a) change any uppercase letter to its lowercase equivalent [2]