Write an HLA Assembly language program that implements a function which correctl
ID: 3664368 • Letter: W
Question
Write an HLA Assembly language program that implements a function which correctly identifies when three passed parameters are in increasing order and returns a boolean value in AL (1 when the three values are in sorted order; 0 otherwise). This function should have the following signature:
procedure isSorted( x: int16; y : int16; z : int16 ); @nodisplay; @noframe;
Shown below is a sample program dialogue.
Feed Me X: 205
Feed Me Y: 170
Feed Me Z: 91
calling isSorted
AL = 0
Feed Me X: 0
Feed Me Y: 1
Feed Me Z: 2
calling isSorted
AL = 1
Feed Me X: 121
Feed Me Y: 100
Feed Me Z: 140
calling isSorted
AL = 0
Explanation / Answer
Find the assembly code below:-
DATA SEGMENT
NUM1 DB ?
NUM2 DB ?
NUM3 DB ?
MSG1 DB 10,13,"ENTER FIRST NUMBER TO COMPARE : $"
MSG2 DB 10,13,"ENTER SECOND NUMBER TO COMPARE : $"
MSG3 DB 10,13,"ENTER THIRD NUMBER TO COMPARE : $"
ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
MOV NUM1,AL
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,1
INT 21H
MOV NUM2,AL
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,1
INT 21H
MOV NUM3,AL
MOV AL,NUM1
CMP AL,NUM2
JGE FALSE --If first number is greater than or equal to second, no need to compare with third number print the result as 0
JL AGAIN --If first number is less than second number, then compare second number with third number
AGAIN: MOV AL,NUM2
CMP AL,NUM3
JG RESULT --If second number is greater than third ,then jump to FALSE
JL TRUE
FALSE: MOV AH,2
MOV DL,"0"
INT 21H
JMP EXIT
TRUE: MOV AH,2
MOV DL,"1"
INT 21H
JMP EXIT
EXIT: MOV AH,4CH
INT 21H
ENDS
END START
-------------------for your reference --
je <label> - Jump when equal
jne <label> - Jump when not equal
jz <label> - Jump when last result was zero
jg <label> - Jump when greater than
jge <label> - Jump when greater than or equal to
jl <label> - Jump when less than
jle <label> - Jump when less than or equal to
explanation:-
First Line – DATA SEGMENT
DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.
Next Line – NUM1 DB ?
NUM2 DB ? NUM3 DB?
MSG1 DB 10,13,”ENTER FIRST NUMBER TO COMPARE : $”
MSG2 DB 10,13,”ENTER SECOND NUMBER TO COMPARE : $”
MSG3 DB 10,13,"ENTER THIRD NUMBER TO COMPARE : $"
We are initializing NUM1 and NUM2 & NUM3 to ? (? stands for blank value), As we are accepting values from User from Console. Detailed explanation is given below.
MSG1 DB 10,13,”ENTER FIRST NUMBER TO COMPARE : $” this line is a declaration of Charater Array initialized with “ENTER FIRST NUMBER TO COMPARE : $”. 10,13, works as New Line Character if this is not present All the Messages will be printed on the same line and $ is used as ( ) NULL character in C program. (A Character is of a BYTE Hence we have to use only DB Define Byte ) and Similarly to MSG2 and MSG3.
Next Line – DATA ENDS
DATA ENDS is the End point of the Data Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Data Segment.
Now, Selection of data type is DB data type the numbers which we are adding will be integers so DB is sufficient.
In Assembly programming, the variable are all defined by bytes only.
DB – Define Byte (Size – 1 Byte)
DW – Define Word (Size – 2 Byte)
DD – Define Double word (Size - 4 Bytes)
DQ – Define Quad word (Size – 8 Bytes)
DT – Define Ten Bytes (Size – 10 Bytes)
NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.
In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).
------>>>>>
In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.
Next Line –CODE SEGMENT
CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.
Next Line – ASSUME DS:DATA CS:CODE
In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )
Next Line – START:
START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.
Next Line – MOV AX,DATA
MOV DS,AX
After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register. MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.
Next Line – LEA DX,MSG1
MOV AH,9
INT 21H
The above three line code is used to print String or Message present in the character Array till $ symbol which tells the compiler to stop.
LEA DX,MSG1 in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element. This same code can be interchangably written as MOV DX, OFFSET MSG1 where OFFSET means effective address and MOV means move second element into the first element.
MOV AH,9
INT 21H
The above two line code is used to PRINT the String or Message of the address present in DX register.
Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h, That means PRINT the String or Message of the address present in DX register.
Next Line – MOV AH,1
INT 21H
MOV NUM1,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM1 in its ASCII form. When you enter 5 we see 35H,So by comparing 30H or 35H we get ASCII value unchanged as we have to print the smaller back to console.
Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 1 or 1h, That means READ a Character from Console, Echo it on screen and save the value entered in AL register.
MOV NUM1,AL means move value in AL register into variable NUM1.
Next Line – LEA DX,MSG2
MOV AH,9
INT 21H
The above two line code is used to PRINT the String or Message of the address present in DX register i.e. for MSG2.
Next Line – MOV AH,1
INT 21H
MOV NUM2,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM2 in its ASCII form.
-----#####---Same for NUM3---###----
Next Line – MOV AL,NUM1
The above line code is to move NUM1 to AL register as we want to compare NUM1 with NUM2.
Others parts of the program are self explanatory...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.