I am supposed to write a conclusion about the following program. I know how the
ID: 671359 • Letter: I
Question
I am supposed to write a conclusion about the following program. I know how the start function was setup, but I am so confused about how the floop function works. Could you write what you understand about the program, please be descriptive.
I wrote down comments of what I understood about the program after the semicolon.
Write a program to copy an 8-bit array (at least 40 elements) from program memory to data memory. After this, swap the upper 4 bits and lower 4 bits of every element. Then find the total number of elements that are between 39 and 117 inclusive.
.include
.cseg
.org 0x00
Jmp start
.org 0xf6
start: ldi ZL, low(array<<1) ;put low byte of the first array element into z pointer
ldi ZH, High(array<<1) ; put high byte of the first array element into z pointer
ldi XH, 0x20 ;put value of 0x2000 into x pointer
ldi XL, 0x00
.equ n1=1 ; initialize the lower array limit
.equ n2=40 ; initialize the higher array limit
.equ n3=0 ; initialize the initial count value
.def ii=r16 ; define register 16 as ii
.def cnt, n3 ; define register 22 as count
floop: cpi ii, n2+1
breq exitloop
inc ii
ipm r18,z+
swap r18
st x+, r18
cpi r18, 39
brsh check
rjmp flop
check: cpi r18, 118
count: inc cnt
rjmp flop
array: .db 0x13,0x23,0x33,0x43,0x53,0x63,0x73,0x83,0x93,0x92
.db 0x14,0x24,0x34,0x44,0x54,0x64,0x74,0x84,0x94,0x95
.db 0x12,0x22,0x32,0x42,0x52,0x62,0x72,0x82,0x94,0x91
.db 0x15,0x25,0x35,0x45,0x55,0x65,0x75,0x85,0x96,0x97
exitloop: rjmp exitloop
Explanation / Answer
Answer:
Please find below the comments on each step of the given program.
.include
.cseg
.org 0x00
Jmp start
.org 0xf6
start: ldi ZL, low(array<<1) ;put low byte of the first array element into z pointer
ldi ZH, High(array<<1) ; put high byte of the first array element into z pointer
ldi XH, 0x20 ;put value of 0x2000 into x pointer
ldi XL, 0x00
.equ n1=1 ; initialize the lower array limit
.equ n2=40 ; initialize the higher array limit
.equ n3=0 ; initialize the initial count value
.def ii=r16 ; define register 16 as ii
.def cnt, n3 ; define register 22 as count
floop: cpi ii, n2+1 ;compare with immediate i.e here it is comparing r16 with n2+1
breq exitloop ;Brach if above two are equal in previous step i.e Branch if Equal
inc ii ;Adds one -1- to the contents of register Rd and places the result in the destination register Rd.
ipm r18,z+
swap r18 ;this will swap high and low nibbles in r18
st x+, r18 ;Store r18 in data space loc (X value post increment).
cpi r18, 39 ;Again comapre r18 with 39
brsh check ;Branch will occur if and only if the unsigned binary number represented in R18 was greater than or equal t0 39
rjmp flop ;Jump to floop function again
check: cpi r18, 118 ;Compare r18 with 118
count: inc cnt ;Increase the counter
rjmp flop ;Jump to floop function again
array: .db 0x13,0x23,0x33,0x43,0x53,0x63,0x73,0x83,0x93,0x92
.db 0x14,0x24,0x34,0x44,0x54,0x64,0x74,0x84,0x94,0x95 ;Initialization of Array
.db 0x12,0x22,0x32,0x42,0x52,0x62,0x72,0x82,0x94,0x91
.db 0x15,0x25,0x35,0x45,0x55,0x65,0x75,0x85,0x96,0x97
exitloop: rjmp exitloop ;EXIT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.