星期三, 2月 04, 2009

EE3120 (B) MP & ASSEMBLY LANGUAGE PROG (02EE3120) Assignment 1 Solution

Question 1 (25 marks)
(a) The 8051 microprocessor is classified as an 8-bit microprocessor. In the controlsignals there are three control signal pins ALE/PROG, PSEN and EA. What are thefunctions of these pins?[6 marks]Ans:ALE/PROG -􀃆Address Latch Enable/ Program Pulse; it is for latching the low byte ofthe address during accesses to external memory. This pin is also the program pulseinput (PROG) during Flash programming.PSEN -􀃆 Program Store Enable; it is the read strobe to external program memoryEA -􀃆 External Memory Access; EA must be strapped to GND in order to enable thedevice to fetch code from external program memory locations starting at 0000H upto FFFFH. [2 each total 6 marks](b) Indicate the source addressing modes of the following 8051 assembly instructions andexplain the operation of each instruction with a suitable diagram:i) MOVC A,@A+PCii) ADD A, R0iii) MOV DPTR,#1200[9 marks]Ans: i) MOVC A,@A+PC is index addressing mode (or code indirect addressing); loadthe content of the effective address stored at PC + value in Accumulator to theAccumulatorii) ADD A, R0 is direct (register) addressing; new content of accumulator is the sum ofold content of accumulator and value stored in register R0iii) MOV DPTR,#1200 is immediate addressing where value of DPTR is loaded with deciamal value of 1200.[1 for address mode and 2 for explanation each and total 9 marks](c) Explain the difference between DB and EQU assembly directives, which one actuallygenerates code?[6 marks]Ans: DB is define byte value and EQU is assigning constant.DB generates codes and EQU does not.[ 2 each total 6 marks](d) Explain the use of subroutine. Describe its process of operation. [4 marks]Ans:⇒ If the same instruction sequence is being used repeatedly, a subroutine is used andcalled by the main program as many times as needed.[1 marks]The operations are⇒ A call to subroutine causes a jump to the address where the subroutine is located andafter executing the subroutine, the program resumes operation at the next instruction(in main program) after the call⇒ The return address must be stored so that the main program can resume after it returnsfrom subroutine⇒ To do this, the stack area of internal RAM is used to automatically store this returnaddress[one each and total 3 marks and max4 marks](a) Explain the mechanism of a stack and describe how a stack is implemented in an8051 microprocessor. Explain how the stack can be used for storing localvariables within the subroutine. What are the advantages of using a stack instoring local variables?[7 marks]Ans:The stack is the memory area that used for storing the return address and all the usedregisters’ contents and the mechanism is based on first in last out when a routine isbeing called.[2 marks]The stack is used in the following steps:⇒ The stack is accessed by a Register SP (stack pointer) which points to the lastused location of the stack.⇒ The default location of is at address 07⇒ SP can be relocated by assigning value with an 8 byte address e.g. MOV SP,#30H⇒ The SP is not only used to access and stack and it points to the last used locationof the stack. The programmer must aware of the largest size of the stack andprevents the stack overflow, i.e. overwritten useful data.[4 marks]The advantages for storing local variables are:⇒ Allow additional temporarily store of registers’ values to avoid modificationswhen manipulation will change registers’ values⇒ Allow restorationof register values when manipulation is restated.[2 marks][1 each total 8 marks](b) Supposing an 8051 microcontroller is used. Show the contents of the stack andstack pointer after execution of the instruction for each line of the followingprogram.ORG 0MOV SP,#90HMOV R4,#1EHMOV R3,#DBHMOV R2,#10HPUSH 4PUSH 3PUSH 2CLR AMOV R2,AMOV R3,APOP 2POP 3POP 4[7 marks]Ans:INSTRUCTIONS SP afterexecution oftheinstructionContents ofthe stackORG 0 07H ?MOV SP,#90H 90H ?MOV R4,#1EH 90H ?MOV R3,#DBH 90H ?MOV R2,#10H 90H ?PUSH 4 91H 1EHPUSH 3 92H DBHPUSH 2 93H 10HCLR A 93H 10HMOV R2,A 93H 10HMOV R3,A 93H 10HPOP 2 92H DBHPOP 3 91H 1EHPOP 4 90H ?[0.5 each total 7 marks](c) Write an 8051 assembler subroutine to search for the maximum value in a tablewith 10 entries. The subroutine should be called by passing the table address andtable size via stack. The returned maximum value can be passed via register. Askeleton C program for your reference is shown below:#define SIZE 10short int table[SIZE] = {3,1,7,4,9,10,2,5,6,8} ;short int max ;// In the main program, the function is called as shown:max = findmax(table, size) ;// This is the findmax functionshort int findmax(short int t[], int s){ // subroutine program body is in here}[10 mark]Ans:ORG 100SIZE EQU 10FINDMAX: MOV Max, #0 ;clear maxMOV DPTR,Table ; assign table pointerMOV R0,#SIZE ;CounterCLR A ; clear AMOV R2,A ; clear R2 stores indexLOOP: MOV A, R2 ; update indexMOV R1, @A+DPTR ; get current data from tableMOV A, Max ; max stores the resultSUB A, R1 ; calculate Max - current dataJNC POSITIVE ; jump if result is positive or 0MOV Max,R1 ; update max if negativePOSITIVE: INC R2 ; increase indexDJNZ R0,LOOP ;repeat all dataORG 200 ;store max resultMax: 0ORG 210 ; store table dataTable: DB 3,1,7,4,9,10,2,5,6,8[0.5 each total 10 marks]

推薦此文