Part A 6 - Lab Program - 6 - Smallest

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Smallest of 32 numbers

6) Write a program to find the largest/smallest number in an array of 32


numbers
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
• Rebuild

• Debug
• Use F10 for step by step executio
Initial status at the start of debug
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Load Array address to R0
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Load Variable Smallest –address to R5
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Initialize loop counter in R1 to 0
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Load the first half word –number into R2
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Compare loop counter with n-1 here it is 9
MOV R2,R3 ; if R3>R2 then change current small to R3 Here 1<9 so BEQ is false so executes next
Skip ADD R1, R1, #1 ; increment the counter by 1 instruction
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip BEQ is false so executes next instruction
MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
• Choose memory to view its content

• Type address of Array in text field ; Address of Array in R0 i.e 00000040


• We can see all data : 0011,0022,0033,0044,0055,0066,0077,0088,0099,00AA
• In little endian format (lower byte lower address ) so 11 first , 00 next…
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip LDRH R3, [R0,#4]! Advance the memory by
MOV R2,R3 ; if R3>R2 then change current small to R3 4 bytes and loads next data into R3
Skip ADD R1, R1, #1 ; increment the counter by 1 00000040+4=00000044
Loop B Next ; repeat for iteration [00000044] to R3= 00000033
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Compare previously assumed Small and next
MOV R2,R3 ; if R3>R2 then change current small to R3 data
Skip ADD R1, R1, #1 ; increment the counter by 1 If Next data is smaller than previous then
Loop B Next ; repeat for iteration change R2 otherwise skip.
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip Compare previously assumed Small and next
MOV R2,R3 ; if R3>R2 then change current small to R3 data
Skip ADD R1, R1, #1 ; increment the counter by 1 If Next data is smaller than previous then
Loop B Next ; repeat for iteration change R2 otherwise skip.
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 (R1- loop counter) is incremented by 1
Skip ADD R1, R1, #1 ; increment the counter by 1 And repeat
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 (R1- loop counter) is incremented by 1
Skip ADD R1, R1, #1 ; increment the counter by 1 And repeat
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
Second Iteratio
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 All numbers are compared?
Skip ADD R1, R1, #1 ; increment the counter by 1 R1 is 9 ? If no repeat
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 Loads next data to R3
Skip ADD R1, R1, #1 ; increment the counter by 1 i.e [00000044] to R3
Loop B Next ; repeat for iteration R3=00000033
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 Compare Smallest( in R2) and R3
Skip ADD R1, R1, #1 ; increment the counter by 1 Change if R2>R3 other proceed for next
Loop B Next ; repeat for iteration iteration…..
STORE STRH R2,[R5] ; store smallest in memory This process is repeated for finding smallest
STOP B STOP
among 10 numbers ..
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3
BLE Skip ; if R2<=R3 (for greater than use GT) skip
MOV R2,R3 ; if R3>R2 then change current small to R3 After comparing all numbers
Skip ADD R1, R1, #1 ; increment the counter by 1 Store smallest in memory
Loop B Next ; repeat for iteration i.e R2 in memory pointed to by R5
STORE STRH R2,[R5] ; store smallest in memory i.e [40000000]=R2=0011
STOP B STOP
We check by changing address to 0x4000000
AREA Data1,DATA, READONLY
In text box.
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END
Biggest of 32 number
For finding biggest among 32 numbers change BLE Skip
by
BGT Skip in the program.
AREA sml1, CODE, READONLY
ENTRY
LDR R0,=ARRAY ; to point to array
LDR R5,=Smallest ; to hold smallest

MOV R1, #0 ; set up the counter


LDRH R2, [R0] ; assume first number as smallest
Next CMP R1, #9 ; Load the total number in the list (n-1)
BEQ STORE ; stop if all numbers are compared and store
LDRH R3, [R0,#2]! ; Load the next data word from the array
CMP R2,R3 ; compare R2 and R3

BGT Skip ; if R2>=R3


MOV R2,R3 ; if R3>R2 then change current small to R3
Skip ADD R1, R1, #1 ; increment the counter by 1
Loop B Next ; repeat for iteration
STORE STRH R2,[R5] ; store smallest in memory
STOP B STOP
AREA Data1,DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099 ,0x00AA
ALIGN
AREA Data2,DATA
Smallest DCW 0
END

You might also like