1

Im creating an 8086 assembly program to ask a user to input a decimal value, add 5 to it and then print. but it performs computations using hexadecimal values.

So when i input 5 for example: it would be 0x35 + 0x35 = 0x6A then Ox6a would printout as 'j' according to the ASCII tables

Im currently using this interrupt for user input.

    mov AH,01H
    INT 21H 

this is my code so far

.MODEL SMALL
.STACK 300
.DATA

MSG DB "Enter a number: $"

msg2 DB 10,13, "Adding 5 to the number...$"

msg3 DB 10,13, "the number is: $"

.CODE


;init data segment
MOV AX,@DATA
MOV DS, AX
MOV ES, AX

;PRINT MSG
LEA DX,MSG
MOV AH,09H
INT 21H

;GET CHAR
mov AH,01H
INT 21H
MOV CL,AL


;PRINT MSG
LEA DX,MSG2
MOV AH,09H
INT 21H

;newl
MOV dl, 10
MOV ah, 02h
INT 21h
MOV dl, 13
MOV ah, 02h
INT 21h

;computations
ADD CL, 0x35

;PRINT MSG
LEA DX,MSG3
MOV AH,09H
INT 21H

;print char
MOV DL,CL
MOV AH,02H
INT 21H







EXIT:
MOV AH,4CH
INT 21H

END

How do i do decimal computations from user input on 8086?

xoox
  • 13
  • 1
  • 7
  • You are very retro computing. Usually you do "binary" calculation and you print the digit in decimal (so converting the numbers). But maybe this is an assignment, and so it is expected that you use BCD (https://en.wikipedia.org/wiki/Intel_BCD_opcode). Could you specify if this is the case? – Giacomo Catenazzi Feb 15 '21 at 09:57
  • I am required to only use interrupts available for 8086 and arithmetic instructions. I will try to look into BCD thanks, – xoox Feb 15 '21 at 10:02
  • But so, you should just do basic conversion from binary to text (BCD could help, but not so much, you should still convert values into strings, BCD allow it with a lot less divisions, and in principle the numbers could be as long as you want) – Giacomo Catenazzi Feb 15 '21 at 10:06
  • sorry, but i do not see how bcd would be able to help me to do conversion when the user input is assumed to be in decimal, can you give me a detailed workflow of what will happen. after a user inputted a value of 5 for example? – xoox Feb 15 '21 at 10:21
  • I think your problem is just "how to convert binary data into a string, in assembler". – Giacomo Catenazzi Feb 15 '21 at 11:22

1 Answers1

1

So when i input 5 for example: it would be 0x35 + 0x35 = 0x6A

You need to make the distinction between the number and its textual representation.

When you input via DOS.GetCharacter function 01h and press 5 on the keyboard, DOS returns an ASCII code that represents the text character "5" (0x35). If you're going to perform any calculations on the inputted number you need to leave the ASCII code behind and start working with the 'true value' which is five. The conversion simply is to subtract 48 (0x30).

add 5 to it

Adding five is just what it is and not adding 0x35 (53) like you wrote it.

;GET CHAR
 mov AH, 01H
 INT 21H
 sub al, 48          ; From character to number
 MOV CL, AL

...

;computations
 ADD CL, 5           ; Calculation on numbers

...

;print result
 cmp cl, 10
 jb  IsSingleDigitResult
IsDoubleDigitResult:
 mov dl, "1"         ; Separately displaying the 'tens'
 mov ah, 02h
 int 21h
 sub cl, 10          ; Isolating the 'ones'
IsSingleDigitResult:
 add cl, 48          ; From number to character
 MOV DL, CL
 MOV AH, 02H
 INT 21H

For all inputs between 5 and 9 the result from adding 5 will consist of 2 digits. Your code must anticipate on this.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76