Skip to content

Instantly share code, notes, and snippets.

@nitwhiz
Last active June 13, 2021 17:14
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 nitwhiz/65f058bb66612b3a023e50e9258df1ac to your computer and use it in GitHub Desktop.
Save nitwhiz/65f058bb66612b3a023e50e9258df1ac to your computer and use it in GitHub Desktop.
; ---
; constants
segm_needle: equ 7 ; digit to display searched value
segm_result: equ 3 ; digit to display counter
; start address
mem_start: equ 0c000h
mem_end: equ 0cfffh
mem_len: equ mem_end - mem_start + 1
; segment display address
segm_addr: equ 90h
; determinate output offsets for display digits
segm_needle_offset: equ segm_needle * 2
segm_result_offset: equ segm_result * 2
org 100h
call display_clear
; ---
; entry point
main:
call read_needle
call display_needle
call counter
call display_result
jmp main
; ---
; read value to be searched for
read_needle:
push ax
in al, 0
mov [needle], al ; set value to be searched for
pop ax
ret
; ---
; count
counter:
push ax
push bx
push cx
push dx
; initialize
mov [result], word 0
mov bx, mem_start
mov ax, 0
mov cx, mem_len
mov dl, [needle]
; loop for counting
iterate:
mov dh, [bx]
cmp dh, dl ; check value at BX
jnz no_hit ; skip increment if value != needle
inc word [result]
no_hit:
inc bx ; next address
loop iterate
pop dx
pop cx
pop bx
pop ax
; ---
; display value to be searched for
display_needle:
push ax
push dx
mov dl, segm_needle_offset ; set display digit
mov al, [needle] ; set display value
call display_2
pop dx
pop ax
ret
; ---
; display result count
display_result:
push ax
push dx
mov dl, segm_result_offset ; set display digit
mov ax, [result] ; set display value
call display_4
pop dx
pop ax
ret
; ---
; display 4 hex numbers
; AX - number to display
; DL - digit to display value at
display_4:
push ax
push cx
push dx
xchg al, ah ; make sure al contains upper 8 bit
call display_2 ; display upper 8 bit
xchg al, ah ; make sure al contains lower 8 bit
sub dl, 4 ; move display digit 2 digits to right
call display_2 ; display lower 8 bit
pop dx
pop cx
pop ax
ret
; ---
; display 2 hex numbers
; AL - number to display
; DL - digit to display value at
display_2:
push cx
push dx
sub dl, 2 ; move display digit right by 1
call display_1 ; display lower 4 bit
mov cl, 4
shr al, cl ; cut off lower 4 bit
add dl, 2 ; move display digit left again
call display_1 ; display upper 4 bit
pop dx
pop cx
ret
; ---
; display 1 hex number
; AL - number to display
; DL - digit to display value at
display_1:
push ax
push bx
push dx
add dl, segm_addr ; add display port address to position
mov bx, codetab ; point to codetab
and ax, 0fh ; mask AX
add bx, ax ; add which number to display from codetab
mov al, [bx] ; copy codetab value
out dx, al ; output codetab value
pop dx
pop bx
pop ax
ret
; ---
; clear display
display_clear:
push ax
push cx
push dx
mov ax, 0
mov dx, segm_addr ; start at display digit 0
mov cx, 8 ; repeat for 7 digits
clear_step:
out dx, ax ; turn off digit
add dx, 2 ; next display digit
loop clear_step
pop dx
pop cx
pop ax
ret
; value to search for
needle:
db 0
; search result
result:
dw 0
codetab:
db 00111111b ; 0
db 00000110b ; 1
db 01011011b ; 2
db 01001111b ; 3
db 01100110b ; 4
db 01101101b ; 5
db 01111101b ; 6
db 00000111b ; 7
db 01111111b ; 8
db 01101111b ; 9
db 01110111b ; A
db 01111100b ; b
db 01011000b ; c
db 01011110b ; d
db 01111001b ; E
db 01110001b ; F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment