Some Assembly Language Examples
1. Describe the action of the following 68K assembly language instructions in RTL, register transfer language. That is, translate the assembly language syntax of the 68K instruction into the RTL notation that defines the action of the instruction.
Code:
a. MOVE 3000,4000 [4000] ← [300] Copy contents of location 3000 to location 4000
b. MOVE D0,D4 [D4] ← [D0] Copy contents of D0 to D4
c. MOVE 3000,D0 [D0] ← [3000] Copy contents of location 3000 to D0
d. MOVE D0,3000 [3000] ← [D0] Copy contents of D0 to location 3000
e. MOVE #4000,D4 [D4] ← 4000 Copy the value 4000 to D4
f MOVE #4000,5000 [50004] ← 4000 Copy the value 4000 to location 5000
g. MOVE (A0),D3 [D3] ← [[A0]] Copy contents of location pointed at by A0 to D3
h. MOVE #12,(A0) [[A0]] ← 12 Copy 12 to location pointed at by A0
i. MOVE (A1),(A2) [[A2]] ← [[A1]] Copy contents of location pointed at by A1 to location pointed at by A2
j. ADD D2,D1 [D1] ← [D1]+[D2] Add contents of D1 to D2 and put sum in D1
k. ADD #13,D4 [D4] ← [D4]+12 Add 13 to D4 and put sum in D4
l. ADD (A3),1234 [1234] ← [[A3]] Add contents of location pointed at by A3 to location 1234
2. Explain why the following assembly language and RTL constructs are incorrect.
Code:
a. MOVE D3,#4 Can't move to a literal destination
b. MOVE [D3],D2 Operand of form [D3] not defined for 68K
c. MOVE (D3),D2 Operand of form (D3) not defined for 68K note (A3) is ok
d. [D3] ← A0 + 3 A0 is an address register should be [A0]
e. [D3] ← #3 Can't use # symbol in RTL
f. 3 ← [D3] Can't have literal as destination in RTL
3. Create a simple 68K program called ADDER. Your program should add together the numbers: 6, 4, 12, 16, 17, and 50. The program is to be assembled with the 68K cross-assembler and then run on the 68K simulator (either Easy68K or the Teesside assembler/simulator). Run the binary file you have created in the simulation mode.
A very simple program that does not use complex addressing modes and only uses very simple instructions is
Code:
ORG $1000 Location of the program
MOVE.B Nmb1,D0 Get first number
ADD.B Numb2,D0 Add in second number
ADD.B Numb3,D0 Add in second number
ADD.B Numb4,D0 Add in second number
ADD.B Numb5,D0 Add in second number
ADD.B Numb6,D0 Add in second number
STOP #$2700 Stop execution
ORG $2000 Location of the data
Nmb1 DC.B 6
Nmb2 DC.B 4
Nmb3 DC.B 12
Nmb4 DC.B 16
Nmb5 DC.B 17
Nmb6 DC.B 50
END $1000 End of program (and address of first instruction)
4. Give examples of valid 68K assembly language instructions that use:
Code:
a. register-to-register addressing MOVE.B D0,D2
b. register-to-memory addressing MOVE.W D0,$1234
c. memory-to-register addressing MOVE.W $1234,D0
d. memory-to-memory addressing MOVE.L (A1),(A3)
6. By means of a memory map explain the effect of the following sequence of 68K assembly language directives.
Code:
ORG $600
DS.L 2
DC.L 2
DC.B '1234'
Time DC.B 6
Top DS.B 6
BSc1 EQU 2
IT1 EQU 3
SE1 DS.B IT1+BSc1
Address Name Value Comment
600 ?????? Reserve 2 x 4 = 8 bytes
608 00000002 Put $00000002 in memory
60C 31 Put the ASCI string '1234' in memory
60D 32
60E 33
60F 34
610 Time 6 Put byte value 6 in memory
611 Top ?? Reserve 6 bytes of storage
BSc1 is 2
IT1 is 3
617 SE1 5 Reserve 5 bytes (IT1+BSc1 is 2+3=5)
61C Next free location
7. What would the following 68K assembly language fragment store at address $1002?
Code:
ORG $1000 Starting point for following data etc is 1000
P EQU 5 P is given the value 5. Using P is the same as using 5
Q DS.B 2 Store two byte locations in memory location 1000 and 1001.
Note that location 1000 is defined as Q
One DC.W P+Q Store a 16-bit word constant in memory location 1002.
The value is P+Q=5+1000 = 1005
8. What is wrong with each of the following 68K assembly language operations?
Code:
a. MOVE Temp,#4 Literal can't be destination
b. ADD.B #1,A3 Can't do byte operation on address register
c. CMP.L D0,#9 Can't have literal as destination
d. MOVE.B #500,D5 Biggest byte move is 255
e. DS.B 1,2 Can't have multiple operands with DS
f. ADD.W +(A2),D3 Can't have preincrementing addressing mode
g. ORG #400 Assembly form � no #. Should be $400
h. BEQ.B Loop_3 No .B extension to branch instructions
9. Answer the following questions about 68K assembler conventions.
a. What is the effect of the assembler directive ORG $400?
Defines the address/location of the next data element of instruction to be loaded
b. What is the effect of the assembler directive DS.W 20?
This directive creates 20 words or 40 bytes of storage from the current location. The next free location is 40 bytes on.
c. What is the effect of the assembler directive DC.W 1234?
The directive DC.W 1234 loads the decimal value 1234 (in binary form) into the current location and moves the location counter on by two bytes.
d. What is the effect of the assembler directive DC.W $1234?
The directive DC.W 1234 loads the hexadecimal value 1234 (in binary form) into the current location and moves the location counter on by two bytes.
e. What is the effect of the + in the effective address (A0)+?
This indicates auto incrementing (more strictly post-incrementing). After the address register pointer has been used, its contents are incremented by 1, 2, or 4 depending on whether the operand was B, W, or L.
f. What is the effect of the - in the effective address -(A0)?
This operand indicated predecrementing and is identical to auto-incrementing except that the address register is decremented before it is used to access the operand.
g. Why ADDA.L #4,A0 and ADD.L #4,D0?
Many modern 68K assemblers don't force you to use this convention. However, the original assembler required you to write ADDA, SUBA, MOVEA, and CMPA if the destination operand was an address register. This was done to remind you that address registers behaved differently to data registers.
11. Translate the following fragment of high-level language into 68K assembly language.
Code:
IF T = 5
THEN X = 4
END_IF
Assume that T and X are in memory. Write a program to implement this fragment of code and run it on the 68K simulator. Select your own values for variables T and X. Use the simulator's trace mode to observe the behavior of the program.
If we are going to turn this fragment of code into a program, we will need to include assembly overheads and data. One possible solution is:
Code:
ORG $400 Start of program
MOVE.B T,D0 Get the value of T
CMP.B #4,T Is the value of T equal to 5
BNE Exit If not then leave
MOVE.B #4,X Else made X = 4
Exit STOP #$2700 Exit point and termination for program
ORG $1000 Put data in here
T DS.B 3 Let's make T = 3 on this run
X DC.B 20 Let's make X = 20
END $400 Last line of prog and starting pt for execution
13. The 68K can operate with byte, word and longword operands. What does this mean? Which type of operand do you use in any particular circumstance?
The 68K's data and registers are 32 bits wide. A data operation may take part on the least-significant 8, 16, or the entire 32 bits of a register (as specified by a .B, >W, or .L suffix to the instruction). The operation ADD.B D0,D1 adds the least significant 8 bits of D0 to the least significant 8 bits of D1 and puts the result in the least-significant bits of D1. Note that bits 8 to 31 of register D1 are not affected by this instruction (some processors allows 8 bit operations but clear higher-order bits).
If operations take one clock cycle, it doesn't matter which format you use. However, if byte or word operations are faster, then it make sense to use smaller data sizes.
However, when dealing with natural 8-bit values such as ASCII characters, 8 bit operations are a good idea because you can read 8-bit values from memory and process them as 8-bit values.
24. Suppose you wish to pre-load memory with the value 1234 before executing a program. Which of the following operations is correct?
Code:
a. DC.B #1234 Incorrect no # and .B wrong
b. DC.W 1234 Correct
c. DC.W #1234 Incorrect no #
d. DS.B $1234 Incorrect this reserves storage
e. MOVE.W #1234,Location Incorrect this loads memory at runtime
25. The register transfer language definition of MOVE.B (A2)+,D3 is one of the following. CHECK
Code:
a. D3 ← [[A2]]; [A2] ← [A2] + 1
b. [D3] ← [[A2]]; [A2] ← [A2] + 1
c. [D3] ← [[A2]]; [A2] ← [A2] + 1
d. [A2] ← [A2] + 1; [D3] ← [A2];
26. When a parameter is passed to a subroutine by reference (i.e., not by value),
a. the parameter can be put in an address register
b. the address of the parameter can be put in an address register
c. the address of the parameter can be pushed on the stack
d. the parameter can be pushed on the stack
e. parts a and d are correct
f. parts b and c are correct CORRECT you pass an address
27. Consider the following code:
Code:
MOVE.W X,-(A7) Push X
MOVE.L Y,-(A7) Push Y
BSR PQR Call PQR
Clean_up Clean up the stack
a. Why do you have to clean up the stack after returning from the subroutine?
b. What code would you use to clean up the stack?
c. Draw a memory map of the stack immediately before executing the RTS in the subroutine PQR.
a. The stack should be balanced in the sense that after you modify it, you should eventually reverse the modification. If a subroutine builds on top of the stack, you should restore the stack before returning.
b. If you push a word and a longword on the stack, you move the stack pointer up by 6 bytes; that is [A7]←[A7] 6. You can undo this by adding 6 to the stack pointer with LEA 6(A7),A7.
c. Assume the stack is, say, $1000 before this code is executed. The first move, moves the stack up by 2 bytes to $0FFE. The second instruction moves the stack pointer up by 4 bytes to $0FFA. The subroutine call saves the 4-byte return address on the stack and pushes up the stack pointer to $0FF6
Code:
0FF6 Ret Return address pushed on stack as a 16-bit word
0FFA YYYY Y pushed on stack as a 32-bit word
0FFE XX X pushed on stack as a 16-bit word
1000 0000 initial stack
28. Write an assembly language program to reverse the bits of a byte.
There are many ways of doing this. One technique is to shift the bits out of a register left (one-by one) and capture them in the eXtend bit. Then to shift the bits of another register right through the eXtend bit to move the bits in but in the reverse order; for example:
Code:
ORG $400
MOVE.B Test,D0 Get the string to reverse
MOVE.B #7,D7 Use D7 as a loop counter for 8 cycles
CLR.L D1 Use D1 to catch the reversed bits
Shft LSL.B #1,D0 Shift the byte one place left catch ms bit
ROXR.B #1,D1
DBRA D7,Shft Repeat 8 times
STOP #$2700 Stop execution
Test DC.B %11001010 Test string
29. Explain why the following assembly language and RTL constructs are incorrect
Code:
a. MOVE D4,#$64
Cannot use literal as destination operand
b. MOVE (D3),D2
Cannot use data register as pointer
c. [D3] ← A0 + 3
This is RTL. Assume A0 should be [A0]
d. [D3] ← #3
This is RTL. The assembly language literal symbol is not required with a literal
32. Assume that a string of ASCII characters is located in memory starting at location $2000. The string ends with the character 'Z'. Design and write a 68K assembly language program to count the number of 'E's, if any, in the string.
Code:
Point to first character
Set Es counter to 0
Loop: Read character at pointer
Point to next character
If character == 'E' then increment Es counter
If character == 'Z' then stop
End loop
ORG $400
LEA Ptr,A0 A0 points at string
CLR.B D0 Use D0 as Es counter assume < 255
Loop MOVE.B (A0)+,D1 Loop: reach char into D1 and update pointer
CMP.B #'Z',D1 test for'E'
BNE NotE if not E then try Z
ADD.B #1,D0 if E then increment Es counter
NotW CMP.B #'Z',D1 test for 'Z'
BNE Loop Repeat until 'Z' found
STOP #$2700 Halt
ORG $1000 Dummy data
Ptr DS.B 'ACDEMEEMDNEEEMEMZ'
END $400
33. Express the following sequence of 68K assembly language instructions in register transfer language and explain in plain English what each instruction does.
Code:
a. LEA 4(A2),A1
Load A1 with the contents of A2 plus 4
[A1] ← [A2] + 4
b. MOVEA.L A3,A2
Copy the contents of A3 into A2
[A2] ← [A3] + 4
c. MOVE.B (A1),D3
Copy the byte at the memory location pointed at by A1 into register D3
[D3] ← [D3] + [[A1]]
d. MOVE.B #5,(A1)
Copy the literal value 5 in to the memory location pointed at by A1
[[A1]] ← 5
e. BCS ABC
If the carry bit is set, jump to the location ABC
IF (C == 1) THEN [PC] ← ABC
f. MOVE.B (A1)+,-(A3)
Move the byte pointed at by A1 to the memory location one less than the location pointed at by A3. Increment A1 by 1 and decrement S3 by 1
[A3] ← [A3] 1
[[A3]] ← [[A1]]
[A1] ← [A1] + 1
35. Suppose you are given an algorithm and asked to design and test a program written in 68K assembly language. How would you carry out this activity? Your answer should include considerations of program design and testing, and the necessary software tools.
The answer to this question is long and depends on the available tools and the complexity of the algorithm to be coded. In principle, for a relatively small algorithm, you would turn the algorithm into pseudocode and then translate the pseudocode fragments into assembly language.
To debug a program you can use a simulator such as EASy68k. You can step through instructions and observe the way in which registers change to see whether the code does something you didn't expect. Moreover, you can add breakpoints (places where execution stops) and run the code until a breakpoint is reached. This helps you when there are thousands of instructions to execute and step-by-step mode would be to slow.