Thursday 31 January 2013

PROGRAM TO DISPLAY A MULTIPLE DIGIT NUMBER IN 8086 ASSEMBLY LANGUAGE


;AUTHOR: ANUEBUNWA VICTOR O.
;PROGRAM TO DISPLAY A MULTIPLE DIGIT NUMBER IN 8086 ASSEMBLY ;LANGUAGE
;ASSEMBLER: MASM611

.MODEL SMALL
.STACK
.DATA
MSG DB 'The multiple digit number is: ','$'
.CODE

START: MOV AX, @DATA
        MOV DS, AX

       MOV AH, 09H
       LEA DX, MSG
       INT 21H         ;Calls MS DOS to display message

       MOV AX, 1234 ;Number to be displayed
       CALL display            ;Calls procedure display to display number

       MOV AH, 4CH
       INT 21H

;Display procedure from previous post should appear here.

END START

;OUTPUT
;The multiple digit number is: 1234

PROCEDURE TO DISPLAY A MULTIPLE DIGIT NUMBER IN 8086 ASSEMBLY LANGUAGE


Good day, Today we will improve on our program from previous post which calculates the average of two numbers in 8086 assembly language to calculate for n numbers, also, you would have noticed that our previous program collects, calculates and displays the average of numbers with a single digit and that doesn't qualify a good program.
Therefore, We will want our program to accept, calculate and display average for numbers with multiple digits.
First we modify our display procedure, making it able to display multiple digits numbers like 10, 80, 345 etc. instead of single characters.
Well, What our new procedure basically does is to split the provided value(in AX) to single digits(i.e by continually dividing it by 10 till it turns 0), change them to their ASCII code equivalence and display them using a loop.

Here is the sample code.
;ASSEMBLER: MASM611
display proc ;Beginning of procedure
MOV BX, 10 ;Initializes divisor
MOV DX, 0000H ;Clears DX
MOV CX, 0000H ;Clears CX

;Splitting process starts here
.Dloop1: MOV DX, 0000H ;Clears DX during jump
div BX ;Divides AX by BX
PUSH DX ;Pushes DX(remainder) to stack
INC CX ;Increments counter to track the number of digits
CMP AX, 0 ;Checks if there is still something in AX to divide
JNE .Dloop1 ;Jumps if AX is not zero

.Dloop2: POP DX ;Pops from stack to DX
ADD DX, 30H ;Converts to it's ASCII equivalent
MOV AH, 02H
INT 21H ;calls DOS to display character
LOOP .Dloop2 ;Loops till CX equals zero
RET ;returns control
display ENDP

Thursday 17 January 2013

PROGRAM TO CALCULATE AVERAGE OF TWO NUMBERS IN 8086 ASSEMBLY LANGUAGE


;AUTHOR: ANUEBUNWA VICTOR O
;PROGRAM TO CALCULATE AVERAGE OF TWO NUMBERS
;USING MASM ASSEMBLER

.MODEL SMALL
.STACK
DATA SEGMENT ; Beginning of data segment
num1 DB 10 ;Declaration of first value
num2 DB 3 ;Declaration of second value
average DB ?         ;Empty slot to store average
remainder DB " remainder ",'$'
rem DB ?         ;Empty slot to store remainder
DATA ENDS ;End of data segment
CODE SEGMENT ; Beginning of code segment
ASSUME CS: CODE, DS: DATA
Start: ;Starting address
MOV AX, DATA
MOV DS, AX ;Initializes the DS(Data segment) register

MOV AL, num1
ADD AL, num2
MOV AH, 00 ; Clears AH Register(Because this is where our remainder will be)
MOV BL, 02 ; Loads divisor into BL register
DIV BL ; DIV : divide AX by BL. Remainder in AH and result in AL

ADD AL, 48
MOV average, AL ;stores average
ADD AH, 48
MOV rem, AH ;stores remainder

CALL display          ;call procedure "display"

MOV AX, 4C00H ;Returns control to MS-DOS
INT 21H

display proc         ;Procedure "display"
MOV AH, 02H
MOV DL, average
INT 21H ;Displays average

MOV AH, 09H
LEA DX, remainder
INT 21H ;Displays message "remainder"

MOV AH, 02H
MOV DL, rem
INT 21H ;Displays remainder
ret ;returns control back to the point this procedure was called
display endp         ;End of procedure



CODE ENDS ;End of code segment
END Start ;End of program

;......................**********.......................................
;OUTPUT>:
;6 remainder 1