Skip to content

Instantly share code, notes, and snippets.

@APIUM
Created July 29, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save APIUM/4187bb9f1732ff32fda01154cdc5efa8 to your computer and use it in GitHub Desktop.
Save APIUM/4187bb9f1732ff32fda01154cdc5efa8 to your computer and use it in GitHub Desktop.
; tableSearch.asm
;
; Created: 21/07/2018 6:49:15 PM
; Author : Glenn Matthews
;
; Define here the variables
.def temp =r16
.def small =r17
.def high_val =r19
.def high_location =r20
; Define here Reset and interrupt vectors, if any
; the only one at the moment is the reset vector
;that points to the start of the program (i.e., label: start)
reset:
rjmp start
reti ; Addr $01
reti ; Addr $02
reti ; Addr $03
reti ; Addr $04
reti ; Addr $05
reti ; Addr $06 Use 'rjmp myVector'
reti ; Addr $07 to define a interrupt vector
reti ; Addr $08
reti ; Addr $09
reti ; Addr $0A
reti ; Addr $0B This is just an example
reti ; Addr $0C Not all MCUs have the same
reti ; Addr $0D number of interrupt vectors
reti ; Addr $0E
reti ; Addr $0F
reti ; Addr $10
; Program starts here after Reset
.cseg
start: ; start here
; The Z register is used as a pointer to the t entries in the table
; so we need to load the location of the table into the high and
; low byte of Z.
; Note how we use the directives 'high' and 'low' to do this.
LDI ZH, high(Tble<<1) ; Initialize Z-pointer
LDI ZL, low(Tble<<1) ; a byte at a time (why <<1?)
LDI small, $FF ; Smallest value (small)
LDI r18, $00 ; Loop conditional
LDI r19, $00 ; Highest Value (high)
LDI r20, $00 ; Location of Highest Value (high_location)
loop:
; ------- Small value
LPM temp, Z ; Load constant from table in memory pointed to by Z (r31:r30)
CP temp, small
BRLO change
; ------- end
; ------- High Value
CP high_val, temp
BRLO higher
; ------- end
INC r18 ; sets up 'for' loop
CPI r18, $40
BREQ here
INC ZL ; move the array pointer
; Note here that we only increment and compare the lower byte
; (very risky - why risky?).
CPI ZL, low(Tble+64<<1) ;Tble+64 is one past the final entry
BRNE loop
change:
MOV small, temp
jmp loop
higher:
MOV high_val, temp
MOV high_location, ZL
jmp loop
here:
jmp here ; this is required to imitate how the design would operate in real life
; Create 64 bytes in code memory of table to search.
; The DC directive defines constants in (code) memory
; you will search this table for the desired entry.
Tble:
.DB 33, 85, 134, 215, 211, 22, 74, 41
.DB 28, 185, 167, 70, 243, 162, 185, 137
.DB 133, 25, 213, 18, 47, 159, 72, 134
.DB 5, 76, 38, 246, 52, 150, 213, 202
.DB 49, 149, 167, 212, 35, 59, 45, 25
.DB 163, 50, 193, 20, 238, 6, 253, 73
.DB 64, 192, 16, 6, 250, 4, 127, 229
.DB 3, 113, 32, 77, 174, 15, 72, 13
.exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment