Find Maximum in Assembly

BIS.B #0xFF,&P1DIR ; Set port1 as output
 BIC.B #0x01,&P1OUT ; clear bit0
 ; Main loop here
start: MOV.W #0, R7 ; initialise max value register
 MOV.W #arr1, R4 ; load the starting address of the array1 into the register R4
loop: MOV.W @R4+,R5 ; load the value stored at the address in register R4 into R5
 CMP.W #0000h,R5 ; check for end of array delimiter '0'
 JZ done ; if end of array jump to done
 CMP.W R7,R5 ; compare current value with max value
 JGE update ; if current value is larger than max value jump to update
 JMP loop

update: MOV.W R5,R7 ; move current value into max value register
 JMP loop ; jump to loop

done: BIS.B #0x01,&P1OUT ; set bit 0 to indicate done
 JMP start ; return to start

arr1: .word 1,2,3,4,5,7,6,0 ; array of 16 bit integers (with '0' delimiter)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s