# need your help to do some arithmetic and output result via the screen

I have recently started programming in 8086 assembly language
using MASM version 5.10 I have come across a program which is giving me a hard time to finish. The program wants the user to enter four numbers via the keyboard and find the sum and average of the four values.
Sum = Digit1 + Digit2 + Digit3 + Digit4
Average = Sum / 4

If necessary the average should be rounded to the next whole number.
The program will then report via the screen the sum of the four values entered and the rounded average value result.

I would very much like your help to finish the program. I am not an experienced programmer in assembly so please provide some comments with your code. Thank you for your time.

• : I have recently started programming in 8086 assembly language
: using MASM version 5.10 I have come across a program which is giving me a hard time to finish. The program wants the user to enter four numbers via the keyboard and find the sum and average of the four values.
: Sum = Digit1 + Digit2 + Digit3 + Digit4
: Average = Sum / 4
:
: If necessary the average should be rounded to the next whole number.
: The program will then report via the screen the sum of the four values entered and the rounded average value result.
:
: I would very much like your help to finish the program. I am not an experienced programmer in assembly so please provide some comments with your code. Thank you for your time.
:
[green]
I don't really have an answer for you, but I've got a lot of free time.
You could post the guts of your code, so others could fix/add to it.
If your input numbers are more than one digit, I would suggest
you save them to memory and then convert ASCII to value.
(add & divide) Single inputs from the keyboard from say
MOV AH,8
INT 21h ; get char in AL without echo to screen.
SUB AL,48 ; = 0 (check for if digit 0-9 first)
that's the easy stuff there.

The type of input? hex binary etc
Use a callable function that converts ASCII numerical strings to value.
I don't have any Masm functions that I can think of handy, but
http://bitdog.home.att.net/files/nasmenv.zip
has a shitpot of functions for ASCII to value and back in INC16
it's a Nasm environment, you might be able to convert some of the
proc's to Masm, or you might like Nasm ?
Any way, one good thing about it is: IT'S FREE.
Bitdog
[/green]

• : : I have recently started programming in 8086 assembly language
: : using MASM version 5.10 I have come across a program which is giving me a hard time to finish. The program wants the user to enter four numbers via the keyboard and find the sum and average of the four values.
: : Sum = Digit1 + Digit2 + Digit3 + Digit4
: : Average = Sum / 4
: :
: : If necessary the average should be rounded to the next whole number.
: : The program will then report via the screen the sum of the four values entered and the rounded average value result.
: :
: : I would very much like your help to finish the program. I am not an experienced programmer in assembly so please provide some comments with your code. Thank you for your time.
: :
: [green]
: I don't really have an answer for you, but I've got a lot of free time.
: You could post the guts of your code, so others could fix/add to it.
: If your input numbers are more than one digit, I would suggest
: you save them to memory and then convert ASCII to value.
: (add & divide) Single inputs from the keyboard from say
: MOV AH,8
: INT 21h ; get char in AL without echo to screen.
: SUB AL,48 ; = 0 (check for if digit 0-9 first)
: that's the easy stuff there.
:
: The type of input? hex binary etc
: Use a callable function that converts ASCII numerical strings to value.
: I don't have any Masm functions that I can think of handy, but
: http://bitdog.home.att.net/files/nasmenv.zip
: has a shitpot of functions for ASCII to value and back in INC16
: it's a Nasm environment, you might be able to convert some of the
: proc's to Masm, or you might like Nasm ?
: Any way, one good thing about it is: IT'S FREE.
: Bitdog
: [/green]
:
:
The code i have written for the Sum and Average program is the following

TITLE Sum and Average

code segment
assume cs:code,ds:data,ss:stack

start: mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov sp,offset top

mov dx,offset welc_message
call PutString

mov dx,offset Get_First_Digit
call PutString
call GetChar
mov first_digit,al
call NewLine

mov dx,offset Get_Second_Digit
call PutString
call GetChar
mov second_digit,al
call NewLine

mov dx,offset Get_Third_Digit
call PutString
call GetChar
mov third_digit,al
call NewLine

mov dx,offset Get_Forth_Digit
call PutString
call GetChar
mov forth_digit,al
call NewLine

mov bh,first_digit
mov ch,second_digit
mov dh,third_digit
mov ah,forth_digit

mov sum,bh

mov dx,offset sum_message
call PutString
mov ah,2
mov dl,sum
int 21h
call NewLine

mov dx,offset average_message
call PutString

mov ax,4c00h
int 21h

GetChar : mov ah,01h
int 21h
ret

PutChar : mov ah,02h
int 21h
ret

PutString: mov ah,09h
int 21h
ret

NewLine : mov ah,2
mov dl,0dh
int 21h
mov dl,0ah
int 21h
ret

code ends

data segment

welc_message db 'This program will calculate the average'
db ' of four values' ,13,10,'\$'

Get_First_Digit db "Please enter the first digit" ,13,10,'\$';

Get_Second_Digit db 'Please enter the second digit' ,13,10,'\$';

Get_Third_Digit db "Please enter the third digit" ,13,10,'\$';

Get_Forth_Digit db 'Please enter the forth digit' ,13,10,'\$';

sum_message db 'The sum of the three original input values'
db ' is ',13,10,'\$';

average_message db "The rounded average value result is ",13,10,'\$';

average db ?
sum db ?
first_digit db ?
second_digit db ?
third_digit db ?
forth_digit db ?

data ends

stack segment stack
db 128 dup(?)
top equ \$
stack ends
end start

The program requests the user to enter four single
digits (0-9)
Input of 1,4,1,3 would equal to 9 (Sum = 9)

Average = Sum / 4
The Average would be rounded up to 3

The above program reports the sum in ASCII notation
so i am trying to write code to convert the sum from
ASCII to Decimal notation and proceed to write the
code for the average.

I have been trying to crack this program for some time now
so any input would be appreciated.Thank you again.

• ; You were getting characters & printing the sum of characters.
; Conversion of char to #value was required.

;TITLE Sum and Average
code segment
assume cs:code,ds:data,ss:stack
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov sp,offset top

mov dx,offset welc_message
call PutString

mov dx,offset Get_First_Digit
call PutString
call GetChar ; & convert to value, check for non# & beep/backspace/try again
mov first_digit,al
call NewLine

mov dx,offset Get_Second_Digit
call PutString
call GetChar
mov second_digit,al
call NewLine

mov dx,offset Get_Third_Digit
call PutString
call GetChar
mov third_digit,al
call NewLine

mov dx,offset Get_Forth_Digit
call PutString
call GetChar
mov forth_digit,al
call NewLine

mov dx,offset sum_message ; message before math
call PutString

XOR AH,AH ; use AX & CX for SUM math
XOR CH,CH
MOV AL,first_digit ; save SUM in word
MOV CL,second_digit
MOV CL,third_digit
MOV CL,forth_digit
MOV SUM,AX ; SUM is a word now
CALL WRITDIGT ; convert value in AX to ASCII & print to stdout at cursor
call NewLine

;mov ah,2 ;this code didn't convert the value of sum
;mov dl,sum ;to an ASCII number to print
;int 21h ;it printed the character of that value

mov dx,offset average_message
call PutString ;there was no math & output code following

MOV AX,SUM
MOV CL,4 ; char input count
CALL CalcAverage ; try CALL Calc2Average
CALL WRITDIGT

MOV DX,OFFSET MASHkey
CALL PutString
call Pause
call NewLine

call PutString
mov ax,4c00h
int 21h
;-----------------------PROC's-----------------

GetChar: mov ah,01h
int 21h
CMP AL,'0' ; is input a number?
JB NONUM
CMP AL,'9'
JA NONUM
SUB AL,'0' ;convert char 0-9 to value 0-9
RET ; return AL= value
NONUM: ; error handler
MOV AH,2
MOV DL,7 ;beep
INT 21h
MOV DL,8 ;backspace
INT 21h
JMP SHORT GetChar

PutChar : mov ah,02h
int 21h
ret

PutString: mov ah,09h
int 21h
ret

Pause: mov ah,08h
int 21h
ret

NewLine : mov ah,2
mov dl,0dh
int 21h
mov dl,0ah
int 21h
ret

CalcAverage:
DIV CL ; divide AX by CL, returns AH=remainder, AL=quotent
OR AH,AH ; check remainder for 0
JZ NoRoundUp
XOR AH,AH
INC AX ; round up
RET ; AX = average rounded up
NoRoundUp:
XOR AH,AH
RET ; AX = average

Calc2Average: ; returns AX=sum rounded up if remainder >= half of number count
DIV CL ; divide AX by CL, returns AH=remainder, AL=quotent
SHR CL,1 ;divide num_count x2
JZ NoRound ;if CL=0 no roundup
CMP AH,CL ;check remainder against num_count/2
JB NoRound ;no round up if, remainder < num_count
XOR AH,AH
INC AX ; round up
RET ; return AX=average rounded up
NoRound:
XOR AH,AH
RET ; return AX=average

[code]
WRITDIGT:
PUSH AX ; Writes a Decimal digit at the cursor & a CRLF after
PUSH BX ; Call with, AX=digit to print
PUSH CX ; Returns, no REG's, prints ascii number to stdout
PUSH DX
XOR CX,CX ; clear count register for push count
MOV BX,10
NZ: XOR DX,DX ; clear the register to push
DIV BX ; divide DX:AX by BX=10
ADD DX,48 ; make remainder number a printable ASCII value
PUSH DX ; put least siginificant number (remainder) on stack
OR AX,AX ; check to see if AX=number is divided to 0 yet
LOOPNE NZ ; get another digit? count the pushes
MOV AH,2 ; function 2 for interupt 21h write digit
NEG CX ; two's compliment, reverse CX
WRID: POP DX ; get digit to print, last pushed=most significant
INT 21h ; print ascii interupt 21h ( function 2 in AH )
LOOP WRID ; deincrement CX, write another, if CX=0 we done
POP DX
POP CX
POP BX
POP AX
RET
[/code]
code ends

data segment
[code]
welc_message db 'This program will calculate the average'
db ' value of four NUMBERS' ,13,10,'\$'
Get_First_Digit db "Please enter the first digit" ,13,10,'\$';
Get_Second_Digit db 'Please enter the second digit' ,13,10,'\$';
Get_Third_Digit db "Please enter the third digit" ,13,10,'\$';
Get_Forth_Digit db 'Please enter the forth digit' ,13,10,'\$';
sum_message db 'The sum of the FOUR original input NUMBERS is ',13,10,36
average_message db "The rounded average value result is ",13,10,'\$';
MASHkey DB 13,10,13,10,"Mash any key......\$"
AdvertizeME db "Visit Bitdog's wild ass web site:",13,10
db "http://bitdog.home.att.net/temp/nasm.txt",13,10,36
[/code]
; average db ?
; sum db ?
first_digit db ?
second_digit db ?
third_digit db ?
forth_digit db ?
EVEN
SUM DW ?
AVERAGE DW ?
data ends

stack segment stack
db 128 dup(?)
top equ \$
stack ends
end start

;I hope this helps somehow
;Bitdog
;PS remove the code & /code in brackets, & assemble

• : ; You were getting characters & printing the sum of characters.
: ; Conversion of char to #value was required.
:
: ;TITLE Sum and Average
: code segment
: assume cs:code,ds:data,ss:stack
: start:
: mov ax,data
: mov ds,ax
: mov ax,stack
: mov ss,ax
: mov sp,offset top
:
: mov dx,offset welc_message
: call PutString
:
: mov dx,offset Get_First_Digit
: call PutString
: call GetChar ; & convert to value, check for non# & beep/backspace/try again
: mov first_digit,al
: call NewLine
:
: mov dx,offset Get_Second_Digit
: call PutString
: call GetChar
: mov second_digit,al
: call NewLine
:
: mov dx,offset Get_Third_Digit
: call PutString
: call GetChar
: mov third_digit,al
: call NewLine
:
: mov dx,offset Get_Forth_Digit
: call PutString
: call GetChar
: mov forth_digit,al
: call NewLine
:
: mov dx,offset sum_message ; message before math
: call PutString
:
: XOR AH,AH ; use AX & CX for SUM math
: XOR CH,CH
: MOV AL,first_digit ; save SUM in word
: MOV CL,second_digit
: MOV CL,third_digit
: MOV CL,forth_digit
: MOV SUM,AX ; SUM is a word now
: CALL WRITDIGT ; convert value in AX to ASCII & print to stdout at cursor
: call NewLine
:
: ;mov ah,2 ;this code didn't convert the value of sum
: ;mov dl,sum ;to an ASCII number to print
: ;int 21h ;it printed the character of that value
:
: mov dx,offset average_message
: call PutString ;there was no math & output code following
:
: MOV AX,SUM
: MOV CL,4 ; char input count
: CALL CalcAverage ; try CALL Calc2Average
: CALL WRITDIGT
:
: MOV DX,OFFSET MASHkey
: CALL PutString
: call Pause
: call NewLine
:
: mov dx,offset AdvertizeMe
: call PutString
: mov ax,4c00h
: int 21h
: ;-----------------------PROC's-----------------
:
: GetChar: mov ah,01h
: int 21h
: CMP AL,'0' ; is input a number?
: JB NONUM
: CMP AL,'9'
: JA NONUM
: SUB AL,'0' ;convert char 0-9 to value 0-9
: RET ; return AL= value
: NONUM: ; error handler
: MOV AH,2
: MOV DL,7 ;beep
: INT 21h
: MOV DL,8 ;backspace
: INT 21h
: JMP SHORT GetChar
:
: PutChar : mov ah,02h
: int 21h
: ret
:
: PutString: mov ah,09h
: int 21h
: ret
:
: Pause: mov ah,08h
: int 21h
: ret
:
: NewLine : mov ah,2
: mov dl,0dh
: int 21h
: mov dl,0ah
: int 21h
: ret
:
: CalcAverage:
: DIV CL ; divide AX by CL, returns AH=remainder, AL=quotent
: OR AH,AH ; check remainder for 0
: JZ NoRoundUp
: XOR AH,AH
: INC AX ; round up
: RET ; AX = average rounded up
: NoRoundUp:
: XOR AH,AH
: RET ; AX = average
:
:
: Calc2Average: ; returns AX=sum rounded up if remainder >= half of number count
: DIV CL ; divide AX by CL, returns AH=remainder, AL=quotent
: SHR CL,1 ;divide num_count x2
: JZ NoRound ;if CL=0 no roundup
: CMP AH,CL ;check remainder against num_count/2
: JB NoRound ;no round up if, remainder < num_count
: XOR AH,AH
: INC AX ; round up
: RET ; return AX=average rounded up
: NoRound:
: XOR AH,AH
: RET ; return AX=average
:
: [code]
: WRITDIGT:
: PUSH AX ; Writes a Decimal digit at the cursor & a CRLF after
: PUSH BX ; Call with, AX=digit to print
: PUSH CX ; Returns, no REG's, prints ascii number to stdout
: PUSH DX
: XOR CX,CX ; clear count register for push count
: MOV BX,10
: NZ: XOR DX,DX ; clear the register to push
: DIV BX ; divide DX:AX by BX=10
: ADD DX,48 ; make remainder number a printable ASCII value
: PUSH DX ; put least siginificant number (remainder) on stack
: OR AX,AX ; check to see if AX=number is divided to 0 yet
: LOOPNE NZ ; get another digit? count the pushes
: MOV AH,2 ; function 2 for interupt 21h write digit
: NEG CX ; two's compliment, reverse CX
: WRID: POP DX ; get digit to print, last pushed=most significant
: INT 21h ; print ascii interupt 21h ( function 2 in AH )
: LOOP WRID ; deincrement CX, write another, if CX=0 we done
: POP DX
: POP CX
: POP BX
: POP AX
: RET
: [/code]
: code ends
:
: data segment
: [code]
: welc_message db 'This program will calculate the average'
: db ' value of four NUMBERS' ,13,10,'\$'
: Get_First_Digit db "Please enter the first digit" ,13,10,'\$';
: Get_Second_Digit db 'Please enter the second digit' ,13,10,'\$';
: Get_Third_Digit db "Please enter the third digit" ,13,10,'\$';
: Get_Forth_Digit db 'Please enter the forth digit' ,13,10,'\$';
: sum_message db 'The sum of the FOUR original input NUMBERS is ',13,10,36
: average_message db "The rounded average value result is ",13,10,'\$';
: MASHkey DB 13,10,13,10,"Mash any key......\$"
: AdvertizeME db "Visit Bitdog's wild ass web site:",13,10
: db "http://bitdog.home.att.net/temp/nasm.txt",13,10,36
: [/code]
: ; average db ?
: ; sum db ?
: first_digit db ?
: second_digit db ?
: third_digit db ?
: forth_digit db ?
: EVEN
: SUM DW ?
: AVERAGE DW ?
: data ends
:
: stack segment stack
: db 128 dup(?)
: top equ \$
: stack ends
: end start
:
: ;I hope this helps somehow
: ;Bitdog
: ;PS remove the code & /code in brackets, & assemble
:
: I have added the code you have written assembled and have a working program now. I appreciate the fact that you made the time to help me out. Thank you again Bitdog.