How to resolve the algorithm Department numbers step by step in the 8086 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Department numbers step by step in the 8086 Assembly programming language

Table of Contents

Problem Statement

There is a highly organized city that has decided to assign a number to each of their departments:

Each department can have a number between   1   and   7   (inclusive). The three department numbers are to be unique (different from each other) and must add up to   12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.

Write a computer program which outputs all valid combinations.

Possible output   (for the 1st and 14th solutions):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Department numbers step by step in the 8086 Assembly programming language

Source code in the 8086 programming language

	cpu	8086
	bits	16
	org	100h
section	.text
	mov	di,obuf		; Output buffer
	mov	bl,2		; BL = police
pol:	mov	cl,1		; CL = sanitation
san:	mov	dl,1		; DL = fire
fire:	cmp	bl,cl		; Police equal to sanitation?
	je	next		; Invalid combination
	cmp	bl,dl		; Police equal to fire?
	je	next		; Invalid combination
	cmp	cl,dl		; Sanitation equal to fire?
	je	next		; Invalid combination
	mov	al,bl		; Total equal to 12?
	add	al,cl
	add	al,dl
	cmp	al,12
	jne	next		; If not, invalid combination
	mov	al,bl		; Combination is valid, write the three numbers
	call	num 
	mov	al,cl
	call 	num
	mov	al,dl
	call 	num
	mov	ax,0A0Dh	; And a newline
	stosw
next:	mov	al,7		; Load 7 to compare to
	inc	dx		; Increment fire number
	cmp	al,dl		; If 7 or less,
	jae	fire		; next fire number.
	inc	cx		; Otherwise, ncrement sanitation number
	cmp	al,cl		; If 7 or less,
	jae	san		; next sanitation number
	inc	bx		; Increment police number twice
	inc	bx		; (it must be even)
	cmp	al,bl		; If 7 or less,
	jae	pol		; next police number.
	mov	byte [di],'$'	; At the end, terminate the string
	mov	dx,ohdr		; Tell MS-DOS to print it
	mov	ah,9
	int	21h
	ret
num:	mov	ah,' '		; Space 
	add	al,'0'		; Add number to output
	stosw			; Store number and space
	ret
section	.data
ohdr:	db	'P S F',13,10	; Header
obuf:	equ	$		; Place to write output


  

You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Ol programming language
You may also check:How to resolve the algorithm Documentation step by step in the Crystal programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Rust programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Python programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the MATLAB / Octave programming language