0

I'm trying to write a program to get a sum of 3 values and getting the mean. So far, I was able to get the sum "correctly" by subtracting a certain value from the sum to get the leading digit of the sum. Subtracting 27h/29 from the sum may not be the most effective method. I know that this may not be the optimal solution, but using division gives me some errors and displays some random symbols as seen on the block I turned into comments.

TITLE Add       (sum.asm)

; This program adds 8-bit values

.model small
.stack 100h
.data
ThreeBytes db 10h, 20h, 30h   ;expected sum is 60h or 96d
message db 0ah, 0dh,"Sum = "
TheSum db ?, 0ah, 0dh, "$"


.code
main PROC
   mov  ax, @data
   mov  ds, ax
   mov  al, ThreeBytes
   add  al, ThreeBytes + 1
   add  al, ThreeBytes + 2
   ;sub al, 40  
   
   ; mov ch, 0
   ; mov ch, al
   ; mov bl, 10
   ; div bl
   
   sub al, 27h ;or 39d to get 9
   mov  TheSum, al ;output is ' (apostrophe) = 60h or 96d

   mov  dx, offset message
   mov  ah, 9

   
   int 21h
   mov ax, 4C00h
   int 21h

main endp
end main

Here is the output that I'm getting from DOSBox: enter image description here

I am a complete beginner with little to no knowledge of assembly language, so I'm hoping for a simple solution/explanation. The code above is a combination of different programs I found, and it does kinda work.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Nico
  • 73
  • 8
  • 1
    Your `div` should work if you `mov ah,0` first, to zero-extend your number in AL into AX. Obviously subtracting alone can't work unless you already know what the answer should be, or if you do *repeated* subtraction to implement division. Other than how to zero-extend, it's once again a duplicate of [Displaying numbers with DOS](https://stackoverflow.com/q/45904075). After zeroing AH, you'll have your number in AX, ready to use the 16-bit number part of that answer. – Peter Cordes Nov 01 '20 at 04:20
  • @Peter Thank you so much! I’m sorry for posting this multiple times. I really appreciate your help. I will try to learn more about assembly so that I could apply what you suggested to my program. – Nico Nov 01 '20 at 13:18
  • 1
    I think the key thing is to understand the *algorithm* for converting a number to a string of decimal digits. (Repeated division by 10 gives you the least significant digit each time). Then you can implement that using the instructions 8086 has, or see how to create suitable input for an implementation someone else has written. You'll know which parts of it are important and can see how it works. Combining pieces of code is much easier when you know how / why they work, so take the time to understand a piece of printing code, *then* you'll know how to use it. – Peter Cordes Nov 01 '20 at 22:16

0 Answers0