Assembly Language using MPLab. Write the code (assembly language for PIC18 \"P18
ID: 3530984 • Letter: A
Question
Assembly Language using MPLab. Write the code (assembly language for PIC18 "P18F452.inc' ) to determine the smallest unsigned byte in the array below, and put the result in 0x50 of the Access file register. array db 14,09,59,52,15,36,07,48,29,11 db 15,22,13,94,45,16,47,28,49,23 db 44,52,63,74,25,26,29,78,39,60 db 31,32,33,34,35,36,37,38,39,41 db 51,42,13,54,45,26,37,42,19,40 EXAMPLE OF ASSEMBLY CODE #include radix dec sum_hi set 0x01 sum_lo set 0x00 lp_cnt set 0x02 kk equ D'50' org 0x00 goto start org 0x08 retfie org 0x18 retfie start movlw kk movwf lp_cnt clrf sum_hi,A clrf sum_lo,A movlw upper(array) movwf TBLPTRU,A movlw high(array) movwf TBLPTRH,A movlw low(array) movwf TBLPTRL,A loop tblrd*+ btfsc TABLAT,0,A goto next movf TABLAT,W,A addwf sum_lo,F,A clrf WREG,A addwfc sum_hi,F,A next decfsz lp_cnt,F,A goto loop nop array db 01,02,03,04,05,06,07,08,09,10 db 11,12,13,14,15,16,17,18,19,20 db 21,22,23,24,25,26,27,28,29,30 db 31,32,33,34,35,36,37,38,39,40 db 41,42,43,44,45,46,47,48,49,50 endExplanation / Answer
; Finds the smallest value in an array.
; This version does not have a findMin procedure
SECTION .data ; start data section
array: dd 25,4,1,2,71,87,17,38,55,13 ; An array of numbers
size: dd 10
msg1: db 'The smallest number in the array is '
len1: equ $-msg1
msg2: db 'The largest number in the array is '
len2: equ $-msg2
msg3: db 'The average of the array is '
len3: equ $-msg3
digits: db '9999999999' ; where decimal digits go
lengthn: equ $-digits
endofstr: db '. ',10 ; new line characters
length2: equ $-endofstr
SECTION .text ; start code (instruction) section
global _start
_start:
; Get the number and put it in the ecx register
pop eax ; number of items
pop eax ; program name
call findMIN
call findMAX
call findAVG
findMIN:
; Find the lowest number in the array.
mov eax, array
mov ebx, [eax]
mov ecx, 1
add eax, 4
findloop:
cmp [size], ecx
je findloopend ; At the end of the array, jump to the end of the loop.
cmp [eax], ebx
jl smaller
jmp lincrement
smaller:
mov ebx, [eax]
lincrement:
inc ecx
add eax, 4
jmp findloop
findloopend:
mov eax, ebx
jmp addition1
findMAX:
mov eax, array
mov ebx, [eax]
mov ecx, 1
add eax, 4
findMAXLOOP:
cmp [size], ecx
je findMAXLOOPend ; At the end of the array, jump to the end of the loop.
cmp [eax], ebx
jg bigger
jmp mincrement
bigger:
mov ebx, [eax]
mincrement:
inc ecx
add eax, 4
jmp findMAXLOOP
findMAXLOOPend:
mov eax, ebx
jmp addition2
findAVG:
mov eax, array
mov ebx, 0 ;start at zero
mov ecx, 1
add eax, 4
AVGLOOP:
cmp [size], ecx
je AVGLOOPend
add ebx, [eax]
inc ecx
add eax,4
jmp AVGLOOP
AVGLOOPend:
mov eax,ebx
mov ebx, 10
push ebx
idiv ebx
jmp addition3
addition1:
; Convert the decimal number to a string
mov ebx,10 ; we will divide by ten
mov ecx,0 ; how many digits to do
mov edi,endofstr-1 ; address of last digit in output string
; This is the place where we will start inserting
; the text digits into the string.
convert1:
mov edx,0 ; clear edx before dividing edx:eax by ebx
div ebx ; result: eax has quotient, edx has remainder
add edx,'0' ; convert byte remainder to single-digit ASCII
mov [edi],dl ; save low-order byte as ASCII in output digits
dec edi ; address of previous digit in output string
inc ecx ; Count the size of the output
cmp eax, 0 ; See if the quotent was zero
; If so, the conversion is finished
jne convert1
push ecx ; Store the ecx value on the stack.
; Write the output string.
mov edx,len1 ; length of string in bytes
mov ecx,msg1 ; address of first byte in string
call write
pop edx ; length of string in bytes
mov ecx, edi ; edi had the address of the beginning
; of the new number to output, minus 1
inc ecx ; Increment ecx to make it the start of the
; string to output
call write
mov edx,length2 ; length of string in bytes
mov ecx,endofstr ; address of first byte in string
call write
mov eax,1 ; unix function code to exit program
mov ebx,0
int 80h
addition2:
; Convert the decimal number to a string
mov ebx,10 ; we will divide by ten
mov ecx,0 ; how many digits to do
mov edi,endofstr-1 ; address of last digit in output string
; This is the place where we will start inserting
; the text digits into the string.
convert2:
mov edx,0 ; clear edx before dividing edx:eax by ebx
div ebx ; result: eax has quotient, edx has remainder
add edx,'0' ; convert byte remainder to single-digit ASCII
mov [edi],dl ; save low-order byte as ASCII in output digits
dec edi ; address of previous digit in output string
inc ecx ; Count the size of the output
cmp eax, 0 ; See if the quotent was zero
; If so, the conversion is finished
jne convert2
push ecx ; Store the ecx value on the stack.
; Write the output string.
mov edx,len2 ; length of string in bytes
mov ecx,msg2 ; address of first byte in string
call write
pop edx ; length of string in bytes
mov ecx, edi ; edi had the address of the beginning
; of the new number to output, minus 1
inc ecx ; Increment ecx to make it the start of the
; string
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.