How to resolve the algorithm Search a list of records step by step in the 8086 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Search a list of records step by step in the 8086 Assembly programming language

Table of Contents

Problem Statement

Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.

If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Search a list of records step by step in the 8086 Assembly programming language

Source code in the 8086 programming language

	.model small
	.stack 1024
	
	.data
Africa     WORD LAGOS   ;"jagged" arrays are the bane of assembly programming, so store the string's pointer here instead.
	   WORD 2100H	;this is a bit cheaty but it's easier to store these as BCD whole numbers
	   WORD CAIRO
	   WORD 1520H
	   WORD KB
	   WORD 1130H
	   WORD GJ
	   WORD 0755H
	   WORD MOGADISHU
	   WORD 0585H
	   WORD KO
	   WORD 0498H
	   WORD DES
	   WORD 0470H
	   WORD ALEXANDRIA
	   WORD 0458H
	   WORD ABIDJAN
	   WORD 0440H
	   WORD CASABLANCA
	   WORD 0398H
	   
LAGOS BYTE "Lagos",0
CAIRO BYTE "Cairo",0
KB BYTE "Kinshasa-Brazzaville",0
GJ BYTE "Greater Johannesburg",0
MOGADISHU BYTE "Mogadishu",0
KO BYTE "Khartoum-Omdurman",0
DES BYTE "Dar Es Salaam",0
ALEXANDRIA BYTE "Alexandria"
ABIDJAN BYTE "Abidjan",0
CASABLANCA BYTE "Casablanca",0
		
	.code
start:

	mov ax,@data		
	mov ds,ax			
	
	mov ax,@code		
	mov es,ax			
	
	
	cld	;String functions are set to auto-increment
	
	mov ax,2  ;clear screen by reloading the video mode we're in
	int 10h	  
	
	mov si,offset Africa
	
;test 1: find the index of the city whose name is Dar-Es-Salaam
	
	mov di,offset DES ;it's easier to test the equality of two pointers than of two strings.
	mov cx,10	  ;ten cities to check
	mov bx,0	  ;our counter
	
test_case_1:
	lodsw
	cmp ax,di		;compare to the pointer of Dar-Es_Salaam
	je done_test_case_1
	add si,2	        ;we know populations aren't going to match so skip them
	inc bx			;increment the counter
	loop test_case_1
	
done_test_case_1:
	mov al,bl
	call Printhex	        ;print the index of Dar-Es-Salaam
	call Newline		;print CRLF
	

;test 2: print the name of the first city whose population is less than 5 million.
	mov si,offset Africa
	mov cx,10
	
	
test_case_2:
	lodsw		;we know that the struct goes city:pop so skip the first word.
	lodsw
	cmp ax,0500h
	jae skip
		sub si,4		;point SI back to the city name
		mov si,[ds:si]
		call PrintString
		call NewLine
		jmp done_test_case_2
	
skip:
	loop test_case_2
done_test_case_2:
	
	
;test 3: find the population of the first city in this list whose name starts with A
	mov si,offset Africa
	mov cx,10
	
test_case_3:
	lodsw
	push si
		mov si,ax
		lodsb
		cmp al,'A'
	pop si
	je FoundIt		;popping SI won't affect the compare result.
	
	add si,2		;skip population
	loop test_case_3
	
	
	

ExitDOS:
	mov ax,4C00h		;return to dos
	int 21h
	
FoundIt:
	lodsw
	mov dx,ax
	mov al,dh
	call Printhex_NoLeadingZeroes
	mov al,'.'             ;we're faking floating point for simplicity's sake
	call PrintChar
	mov al,dl
	call PrintHex
	jmp ExitDos
	
end start


  

You may also check:How to resolve the algorithm XML/DOM serialization step by step in the Clojure programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Wren programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Fortran programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the Sidef programming language
You may also check:How to resolve the algorithm Copy a string step by step in the AArch64 Assembly programming language