1/ 13/ 13
Som e Assem bly Language Exam ples
Some Assembly Language Examples 1.
2.
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.
a. MOVE 4000 b. MOVE c. MOVE d. MOVE e. MOVE f MOVE 5000 g. MOVE to D3 h. MOVE i. MOVE
3000,4000
[4000] ← [300]
Copy contents of location 3000 to location
D0,D4 3000,D0 D0,3000 #4000,D4 #4000,5000
[D4] ← [D0] [D0] ← [3000] [3000] ← [D0] [D4] ← 4000 [50004] ← 4000
Copy contents of D0 to D4 Copy contents of location 3000 to D0 Copy contents of D0 to location 3000 Copy the value 4000 to D4 Copy the value 4000 to location
(A0),D3
[D3] ← [[A0]]
Copy contents of location pointed at by A0
#12,(A0) (A1),(A2)
j. k. l.
D2,D1 #13,D4 (A3),1234
[[A0]] ← 12 Copy 12 to location pointed at by A0 [[A2]] ← [[A1]] Copy contents of location pointed at by A1 to location pointed at by A2 [D1] ← [D1]+[D2] Add contents of D1 to D2 and put sum in D1 [D4] ← [D4]+12 Add 13 to D4 and put sum in D4 [1234] ← [[A3]] Add contents of location pointed at by A3 to location 1234
Explain why the following assembly language and RTL constructs are incorrect.
a. b. c. d. e. f. 3.
ADD ADD ADD
MOVE D3,#4 MOVE [D3],D2 MOVE (D3),D2 [D3] ← A0 + 3 [D3] ← #3 3 ← [D3]
Can't move to a literal destination Operand of form [D3] not defined for 68K Operand of form (D3) not defined for 68K � note (A3) is ok A0 is an address register � should be [A0] Can't use # symbol in RTL Can't have literal as destination in RTL
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 ORG $1000 MOVE.B Nmb1,D0 ADD.B Numb2,D0 ADD.B Numb3,D0 ADD.B Numb4,D0 ADD.B Numb5,D0 ADD.B Numb6,D0
www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
not use complex addressing modes and only uses very simple instructions is Location of the program Get first number Add in second number Add in second number Add in second number Add in second number Add in second number 1/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
Nmb1 Nmb2 Nmb3 Nmb4 Nmb5 Nmb6
4.
#$2700 $2000 6 4 12 16 17 50 $1000
Stop execution Location of the data
End of program (and address of first instruction)
Give examples of valid 68K assembly language instructions that use: a. b. c. d.
6.
STOP ORG DC.B DC.B DC.B DC.B DC.B DC.B END
register-to-register addressing register-to-memory addressing memory-to-register addressing memory-to-memory addressing
MOVE.B D0,D2 MOVE.W D0,$1234 MOVE.W $1234,D0 MOVE.L (A1),(A3)
By means of a memory map explain the effect of the following sequence of 68K assembly language directives. 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
7.
Address 600 608 60C memory 60D 60E 60F 610 611
Name
Value ?????? 00000002 31
Time Top
32 33 34 6 ??
617 2+3=5) 61C
SE1
5
Comment Reserve 2 x 4 = 8 bytes Put $00000002 in memory Put the ASCI string '1234' in
Put byte value 6 in memory Reserve 6 bytes of storage BSc1 is 2 IT1 is 3 Reserve 5 bytes (IT1+BSc1 is Next free location
What would the following 68K assembly language fragment store at address $1002?
P Q
www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
ORG $1000 EQU 5 DS.B 2
Starting point for following data etc is 1000 P is given the value 5. Using P is the same as using 5 Store two byte locations in memory location 1000 and 1001. Note that location 1000 is defined as Q 2/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
One
8.
Store a 16-bit word constant in memory location 1002. The value is P+Q=5+1000 = 1005
What is wrong with each of the following 68K assembly language operations? a. b. c. d. e. f. g. h.
9.
DC.W P+Q
MOVE ADD.B CMP.L MOVE.B DS.B ADD.W ORG BEQ.B
Temp,#4 #1,A3 D0,#9 #500,D5 1,2 +(A2),D3 #400 Loop_3
Literal can't be destination Can't do byte operation on address register Can't have literal as destination Biggest byte move is 255 Can't have multiple operands with DS Can't have preincrementing addressing mode Assembly form � no #. Should be $400 No .B extension to branch instructions
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,A0and 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. 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: www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
3/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
ORG MOVE.B CMP.B BNE MOVE.B Exit STOP
T X
$400 T,D0 #4,T Exit #4,X #$2700
Start of program Get the value of T Is the value of T equal to 5 If not then leave Else made X = 4 Exit point and termination for program
ORG DS.B DC.B
$1000 3 20
Put data in here Let's make T = 3 on this run 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 higherorder 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? a. b. c. d. e.
DC.B DC.W DC.W DS.B MOVE.W
#1234 1234 #1234 $1234 #1234,Location
Incorrect � Correct Incorrect � Incorrect � Incorrect �
no # and .B wrong no # this reserves storage this loads memory at runtime
25. The register transfer language definition of MOVE.B (A2)+,D3is one of the following. CHECK a. b. c. d.
D3 ← [[A2]]; [A2] ← [A2] + 1 [D3] ← [[A2]]; [A2] ← [A2] + 1 [D3] ← [[A2]]; [A2] ← [A2] + 1 [A2] ← [A2] + 1; [D3] ← [A2];
26. When a parameter is passed to a subroutine by reference (i.e., not by value), a. b. c. d. e. f.
the parameter can be put in an address register the address of the parameter can be put in an address register the address of the parameter can be pushed on the stack the parameter can be pushed on the stack parts a and d are correct parts b and c are correct CORRECT � you pass an address
27. Consider the following code: www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
4/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
MOVE.W X,-(A7) MOVE.L Y,-(A7) BSR PQR Clean_up a. b. c.
Push X Push Y Call PQR Clean up the stack
Why do you have to clean up the stack after returning from the subroutine? What code would you use to clean up the stack? 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 0FF6 0FFA 0FFE 1000
Ret YYYY XX 0000
Return address pushed on stack as a 16-bit word Y pushed on stack as a 32-bit word X pushed on stack as a 16-bit word 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:
Shft
Test
ORG MOVE.B MOVE.B CLR.L LSL.B ROXR.B DBRA STOP DC.B
$400 Test,D0 #7,D7 D1 #1,D0 #1,D1 D7,Shft #$2700 %11001010
Get the string to reverse Use D7 as a loop counter for 8 cycles Use D1 to catch the reversed bits Shift the byte one place left � catch ms bit Repeat 8 times Stop execution Test string
29. Explain why the following assembly language and RTL constructs are incorrect 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.
www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
5/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
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. 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 www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
6/ 7
1/ 13/ 13
Som e Assem bly Language Exam ples
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.
www. scm . t ees. ac. uk/ user s/ u0000408/ CSY/ 68Kexam ples. ht m
7/ 7