This is assembly code that toggles all the bits of Port B by sending 55H and AAH
ID: 1845998 • Letter: T
Question
This is assembly code that toggles all the bits of Port B by sending 55H and AAH continuously. There is a time delay between each issuing of data to port B.
PORTB EQU 0x0F81 ; Code added for assembly purpose
MYREG EQU 0x08
ORG 0
BACK MOVLW 0x55
MOVWF PORTB
CALL DELAY
MOVLW 0xAA
MOVWF PORTB
CALL DELAY
GOTO BACK
ORG 300H
DELAY MOVLW 0xFF
MOVWF MYREG
AGAIN NOP
NOP
DECF MYREG, F
BNZ AGAIN
RETURN
END
The code in the book starts from line 2 till line 19. Line 1 to define PORTB was added so that it assembles in MPLAB IDE. Look at book page 112 so that you can see the comments after each instruction.
(7%) When you run this code in MPLAB IDE, the first line run is line 4 BACK MOVLW 0x55. Why it is not line 2 or line 3, or line 5, 6 etc.)? Explain!
(8%) The next three lines executed are: line 5 MOVWF PORTB, line 6 CALL DELAY, and then line 12 DELAY
MOVLW 0xFF. Why line 7 is not executed after line 6? Explain!
(15%) The first 4 lines executed are lines 4, 5, 6, and 12. What will be the next 20 lines executed? Explain! Notice there is a loop from line 17 BNZ AGAGIN to line 14 AGAIN NOP. Notice also line 16 DECF MYREG,F that decreases the value in MYREG.
(15%) Is line 7 MOVLW 0xAA executed in the first 1, 000 lines? Use some calculation to justify your argument. Is line 18 RETURN executed in the first 1,000 lines?
Explanation / Answer
1) The program is compiled by the assembler line after line starting with line one. However, the first 3 lines (1,2,3) are ASSEMBLER DIRECTIVES and not INSTRUCTION CODES. Assembler directives tell the asembler to reserve some space into memory, to define the value of ome constant variables (like in line 1 and 2, variables portb and myreg are assigned the values 0f81 and 08) and to start place the resulting compiled code at some memory address (like line 3 that is telling asembler to place the code at address 0 in memory).
Line 4 is the first a real intruction code. This is why it is executed first.
Line 5,6,7 are following line 4 and as said the assembler compile them one after the other starting with line 4.
2) The next lines after 4 are 5 and 6. Line 5 move constant portb to f register and line 6 calls a subroutine named DELAY. The subroutine starts at line 12, this is why line 12 is executed after line 6. (Line 7 is not executed after 6) because line 6 contains a call to a function that starts at line 12.)
3) the next 20 lines executed after line 12 are
13, 14, 15, 16, 17
14, 15,16,17
14,15,16, 17
14,15,16, 17
14,15,16
Explanation: in line 16 the value of myreg variable is decremented by one and stored into f. If the result of decrementing is not zero in line 17 there will be a branch back on line 14 (BNZ means branch if not zero). Because the intial value of myreg was 8 the loop (14, 15,16, 17) is executed 8 times.
4) yes line 7 is executed in the first 1000 lines.
As said above the loop in the DELAY subroutine is executed only 8 times. The loop contains 4 lines so there are a total of 4*8 (loop lines)+ 3 (lines 12, 13 and 18) = 35 lines in the soubroutine. The entire soubroutine is executed only once before line 7, in line 6.
5) Line 18 is the return statement from the soubroutine. It is the last line of the soubroutine DELAY executed and causes the progarm to jump (branch) back to tle CALL intruction that originally called the soubroutine. Since the soubroutine was called first in line 6 and it will execute only 35 (the last line being 18-return) lines (see above) then line 18 Return, will be executed in the first 1000 lines of code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.