Array

Searching in Array (consecutive memory locations)
; this program gives the index of the digit in array
; this code also shows how to use an array
org 100h

array db 6, 9, 7, 8, 5 ;here an array is made these are actually consective memory locations
;I have used the word array for readibility you can change it with any other name
key db 3  ; this is the key which we are looking for
count db 6 ;count is equal to length of array

mov bl,0;starting index is zero
mov si,48;print zero if not found, ascii of zero is 48
loop:
dec [count]
jz end ;when count is zero jump to label named end

mov al,[key]
cmp al,[array+bx]
je found;if equal jump to label found
add bx,1 ;next index
jmp loop

found:

add bx,49;to get ascii of index we add ascii code of 1 i.e 49
mov si,bx;index will be moved to si
 ;if not found si will reamin zero
end:
mov dx , si
    mov ah , 2h
    int 21h   ;printing
   
MOV AH,4CH;terminating
INT 21H


If I change value of key to be any from the array then output will be the index


Call functions from array

;this program first loads the offsets of functions in an array than
;traverses the array calling all those funcitons
org 100h 

jmp start

arr dw 0,0,0 ;intially we put zeros latter we will put offsets of functions
count dw 0

start:

mov ax , offset fun1;moving offset of fun1 to ax
call add_func

mov ax , offset fun2;moving offset of fun2 to ax
call add_func

mov ax , offset fun3;moving offset of fun3 to ax
call add_func

;here calling the sub routine which will call all sub routines from array
call call_subroutines

end:
MOV AH,4CH 
INT 21H

call_subroutines:;this function will call other functions
mov bx,0
fun_call:
    call [arr+bx]
    add bx,2 ;for moving to next index of array 
    cmp bx,6
jne fun_call
ret

add_func:;this function puts offset into array
mov bx,[count]
mov [arr+bx], ax
add [count], 2;because our array is of word not byte
ret

fun1:;prints 1
mov dx , '1' 
mov ah , 2h 
int 21h 
ret

fun2:;prints 2
mov dx , '2' 
mov ah , 2h 
int 21h 
ret

fun3:;prints 3
mov dx , '3' 
mov ah , 2h 
int 21h 
ret


No comments:

Post a Comment