1. Write a program to find the sum of the elements of an array called list1. The
ID: 2988630 • Letter: 1
Question
1. Write a program to find the sum of the elements of an array called list1. The size of list1 is four bytes. The values of list1 are $FF, $1, $FE, and $02. To check your work, the sum should become $0200.
The requirements for writing this program are as follows.
-You must use a loop and count variable (i.e. count must be defined in the data section) as a loop counter.
- Clear both Registers A and B using CLRA and CLRB instructions.
-Use indexed addressing mode to access the elements of the array using Register X as the index register. The instruction LDX #list1 places the array list1 address into Register X.
-Add each element to Register B.
-Use the branch instruction BCC to check if there is a carry after the addition of each element. BCC means the Branch if Carry flag is clear.
-If there is a carry, add 1 to Register A.
-The final result should be in Register A and B.
Explanation / Answer
;*****************************************************************
;* This stationery serves as the framework for a *
;* user application (single file, absolute assembly application) *
;* For a more comprehensive program that *
;* demonstrates the more advanced functionality of this *
;* processor, please see the demonstration applications *
;* located in the examples subdirectory of the *
;* Freescale CodeWarrior for the HC12 Program directory *
;*****************************************************************
; export symbols
XDEF Entry, _Startup ; export 'Entry' symbol
ABSENTRY Entry ; for absolute assembly: mark this as application entry point
; Include derivative-specific definitions
INCLUDE 'derivative.inc'
RAMStart EQU $2000 ; absolute address to place variables
RAMEnd EQU $3FFF ; last address of RAM, used to init stack pointer
ROMStart EQU $4000 ; absolute address to place code/constant data
; variable/data section
ORG RAMStart
; Insert here your data definition.
list1 DC.B $FF, $1, $FE, $02
cnt DCW 4
; code section
ORG ROMStart
Entry:
_Startup:
LDS #RAMEnd+1 ; initialize the stack pointer
CLRA;
CLRB;
LDX #list1 ; X -> address of list1
LDY #cnt ; Y -> address of cnt
LDY 0,Y ; Y -> value of cnt
Loop
ADDB 0,X ; B = B + X[0]
INX; increment the pointer to list1
BCC Jump;
INCA;
Jump
DEY; decrement the cnt value
BNE Loop ; loop again of the cnt is not completed
forever
BRA forever; forever loop
RTC ; return to caller
RTS ; result in D
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ; Reset Vector
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.