COSC 2325.S01
Program Eight
Professor Mavis Merrywether is teaching a graduate-level seminar on tropical diseases. Professor Merrywether is a respected scholar in the field, but she never really grasped arithmetic. After giving the class a test, she would like to know what the average test grade was. The test grades were as follows:
52, 77, 51, 89, 98, 74 ,85, 92, 95, and 86
Write an assembly procedure to calculate the average grade and display it.
The average grade for test 1 is
Then, write an assembly procedure to find the highest test grade from that array of grades and display it. (Professor Merrywether can certainly determine this on her own, but you think a bit of extra work will make her like you better.)
The highest grade on test 1 is
Hi
this is program of average of n numbers
DATA SEGMENT
ARRAY DB 52, 77, 51, 89, 98, 74 ,85, 92, 95, 86
AVG DB ?
MSG DB "AVERAGE = $"
ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA SI,ARRAY
LEA DX,MSG
MOV AH,9
INT 21H
MOV AX,00
MOV BL,9
MOV CX,9
LOOP1:
ADD AL,ARRAY[SI]
INC SI
LOOP LOOP1
DIV BL
ADD AL,30H
MOV DL,AL
MOV AH,2
INT 21H
MOV AH,4CH
INT 21H
ENDS
END START
OUTPUT ************ Z:\SYSTEM~1\AS1>ex11 ENTER HOW MANY NO U WANT:10 ENTER NO:52 ENTER NO:77 ENTER NO:51 ENTER NO:89 ENTER NO:98 ENTER NO:74 ENTER NO:85 ENTER NO:94 ENTER NO:92 ENTER NO:95 ENTER NO:86 AVEARGE:79
for 2nd quetion
largest element in an array is
DATA SEGMENT ARR DB 52, 77, 51, 89, 98, 74 ,85, 92, 95, 86 LEN DW $-ARR LARGE DB ? DATA ENDS CODE SEGMENT ASSUME DS:DATA CS:CODE START: MOV AX,DATA MOV DS,AX LEA SI,ARR MOV AL,ARR[SI] MOV LARGE,AL MOV CX,LEN REPEAT: MOV AL,ARR[SI] CMP LARGE,AL JG NOCHANGE MOV LARGE,AL NOCHANGE: INC SI LOOP REPEAT MOV AH,4CH INT 21H CODE ENDS END START
Output will be
ARR :52, 77, 51, 89, 98, 74 ,85, 92, 95, 86
Largest: 98
Explanantion
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 SI,ARR
LEA SI,ARR 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 PRICE where OFFSET means effective address and MOV means move second element into the first element. Here Base Address of variable PRICE is loaded in DX register.
Next Line – MOV AL,ARR[SI]
MOV LARGE,AL
MOV AL,ARR[SI] means move value of Array ARR in index of SI register to AL register. MOV LARGE,AL is to move AL register (First value in Array) to LARGE variable as we want to compare it with All the Array elements.
Next Line – MOV CX,LEN
MOV CX,LEN is used to move or assign value 8 (Length of Array) to CX. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).
Next Line – REPEAT:
REPEAT: is a LABEL and all the words ending in colon (:).
Next Line – MOV AL,ARR[SI]
MOV AL,ARR[SI] is to move value of Array ARR in index of SI register to AL register (Different values in Array). As we want to compare All elements with variable LARGE.
Next Line – CMP LARGE,AL
JG NOCHANGE
CMP LARGE,AL is used to compare AX register with 9 and JG NOCHANGE jump if AL is greater to the respective LABEL NOCHANGE. The result of Comparision is not stored anywhere, but flags are set according to result. is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP.
Next Line – MOV LARGE,AL
MOV LARGE,AL is to move AL register (larger value in Array) to LARGE variable.
Next Line – NOCHANGE:
NOCHANGE: is a LABEL and all the words ending in colon (:).
Next Line – INC SI
INC SI will increment the Address value present in SI register. Here we are using SI register as a SOURCE INDEX which holds the Address of Array elements to Cover all the elements in Array.
Next Line – LOOP REPEAT
LOOP REPEAT This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).
Next Line – EXIT: MOV AH,4CH
INT 21H
The above two line code is used to exit to dos or exit to operating system. 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 4ch, That means Return to Operating System or DOS which is the End of the program.
Next Line – CODE ENDS
CODE ENDS is the End point of the Code 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 Code Segment.
Last Line – END START
END START is the end of the label used to show the ending point of the code which is written in the Code Segment.
Hence this my code
if you are still unable to understand
then please ask your doubt in commrnt section
I will try my best to answer it.
Thank You
Happy Learning
COSC 2325.S01 Program Eight Professor Mavis Merrywether is teaching a graduate-level seminar on tropical diseases. Professor...