Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Good evening! thanks for reading. I wrote this short code, it should read and sum two one digit numbers and I used the simulator emu8086 but it doesn't work at all, would you tell me why? I would appreciate it a lot. Thanks in advance.

What I have tried:

.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB ?
NUM2 FB ?
DOM1 DB ‘digit the first number : ‘,13, 10 , ‘$’
DOM2 DB ‘digit the second one: : ‘,13, 10 , ‘$’
RIS1 DB ‘la somma: ’, 13,10, ‘$’
SOMMA DB ?
.CODE
.STARTUP
MOV AX, @DATA
MOV DS, AX
MOV AH, 09H
MOV DX, OFFSET DOM1
INT 21H
MOV AH, 01H
INT 21H
MOV NUM1, AL

MOV AH, 09H
MOV DX, OFFSET DOM2
INT 21H

MOV AH, 01H
INT 21H
MOV NUM2, AL

MOV AL, NUM1
MOV AL, NUM2
MOV SOMMA, AL 

MOV AH, 09H
MOV DX, OFFSET RIS1
INT 21H

SUB SOMMA, 30H

MOV AH, 09H
MOV DX, OFFSET RIS1
INT 21H
 
SUB SOMMA, 30H

MOV AH,02H
MOV DL, SOMMA
INT 21H

MOV AH, 40H
INT 21H 

END


[edit]
at this point , I am here:
ASM
.MODEL SMALL
.STACK 100H .DATA
NUM1 DW ?
NUM2 DW ?
DOM1 DB 'digit the number: ',13, 10 , '$'
DOM2 DB 'digit the other one: ',13, 10 , '$'
RIS1 DB 'the sum: ', 13,10, '$'
SOMMA WB ?
.CODE
.STARTUP
MOV AX, @DATA
MOV DS, AX
MOV AH, 09H
MOV DX, OFFSET DOM1
INT 21H
MOV AH, 01H
INT 21H
MOV NUM1, AL

MOV AH, 09H
MOV DX, OFFSET DOM2
INT 21H

MOV AH, 01H
INT 21H
MOV NUM2, AL  


MOV AH, 01H
INT 21H
XOR AH,AH  ; clear AH
SUB AL,'0' ; make it integer
MOV NUM1,AX

MOV AH, 01H
INT 21H
XOR AH,AH  ; clear AH
SUB AL,'0' ; make it integer
MOV NUM2,AX


MOV AL, NUM1
MOV AL, NUM2
MOV SOMMA, AL 

MOV AH, 09H
MOV DX, OFFSET RIS1
INT 21H

SUB SOMMA, 30H

MOV AH, 09H
MOV DX, OFFSET RIS1
INT 21H
 
SUB SOMMA, 30H

MOV AH,02H
MOV DL, SOMMA
INT 21H

MOV AH, 40H
INT 21H 

END


it still doesn't work but thanks to you I have modified something.. that MOV NUM1, AL creates some issues..but it should be right..the compiler says that the operands don't match..:(

[/edit]
Posted
Updated 2-Oct-22 1:48am
v3
Comments
k5054 1-Oct-22 13:47pm    
"It doesn't work at all".
Does it crash?
Are there assembly errors?
Does it produce the wrong output?
Does it hang?
Does it never stop?

Please be more specific about what "doesn't work at all" means.
Member 15784866 1-Oct-22 14:04pm    
hello, thanks for answering! you are right, I beg your pardon. The emulator shows "the emulator is halted." before giving the final result
Member 15784866 1-Oct-22 14:05pm    
there is an "FB" instead of "DB", this is an error, but not the main one

Do you see something wrong here?
ASM
MOV AL, NUM1
MOV AL, NUM2
MOV SOMMA, AL 

And at that point both NUM1 and NUM2 are characters, and not numeric values. So the first thing you need to do is convert each number to its integer equivalent. So change NUM1 and NUM2 to DW type and save them by:
ASM
MOV AH, 01H
INT 21H
XOR AH,AH  ; clear AH
SUB AL,'0' ; make it integer
MOV NUM1,AX

Then do the same with NUM2. But when you add them together the total could be greater than 9, so you need to code the algorithm to convert an integer into printable digits. And you could save duplication by using subroutines for capturing the numbers.

[edit]
There are a few issues here
ASM
  1  .MODEL SMALL
  2  .STACK 100H .DATA NUM1 DW ?
  3  NUM2 DW ?
  4  DOM1 DB 'digit the number: ',13, 10 , '$'
  5  DOM2 DB 'digit the other one: ',13, 10 , '$'
  6  RIS1 DB 'the sum: ', 13,10, '$'
  7  SOMMA WB ? ; ***** WB should be DW
  8  .CODE
  9  .STARTUP
 10  MOV AX, @DATA
 11  MOV DS, AX
 12  MOV AH, 09H
 13  MOV DX, OFFSET DOM1
 14  INT 21H
 15  MOV AH, 01H  ;
 16  INT 21H      ; ***** You are trying to store a single character into a word
 17  MOV NUM1, AL ;
 18  
 19  MOV AH, 09H
 20  MOV DX, OFFSET DOM2
 21  INT 21H
 22  
 23  MOV AH, 01H  ;
 24  INT 21H      ; ***** same as NUM1 above
 25  MOV NUM2, AL ;
 26  
 27  
 28  MOV AH, 01H  ; ***** these five lines should have replaced lines 15-17
 29  INT 21H
 30  XOR AH,AH  ; clear AH
 31  SUB AL,'0' ; make it integer
 32  MOV NUM1,AX
 33  
 34  MOV AH, 01H  ; ***** and these five should have replaced 23-25
 35  INT 21H
 36  XOR AH,AH  ; clear AH
 37  SUB AL,'0' ; make it integer
 38  MOV NUM2,AX
 39  
 40  
 41  MOV AL, NUM1  ; ***** You are treating a word as a byte
 42  MOV AL, NUM2  ; ***** this overwrites AL
 43  MOV SOMMA, AL ; ***** you are trying to move the last character to the sum
 44  
 45  MOV AH, 09H
 46  MOV DX, OFFSET RIS1
 47  INT 21H
 48  
 49  SUB SOMMA, 30H ; ***** what is this for?
 50  
 51  MOV AH, 09H
 52  MOV DX, OFFSET RIS1
 53  INT 21H
 54   
 55  SUB SOMMA, 30H ; ***** and what is this for?
 56  
 57  
 58  MOV AH,02H
 59  MOV DL, SOMMA
 60  INT 21H
 61  
 62  MOV AH, 40H
 63  INT 21H 
 64  
 65  END


You need to stop coding and write down on paper the steps needed to get this program working. Print the message that asks for a digit, and read the digit from the user. You then convert the digit from a character to a binary integer, and save it in a variable defined as a DW type. Once you have two digits then you should add them together and store the result in the SOMMA variable. But you cannot print a binary value directly so you need to write a function that converts it to its character representation - and that is a good exercise for you to use your assembler skills. So think slowly what you are trying to achieve and try to make your code follow the logical steps.
[edit]
 
Share this answer
 
v2
Comments
CPallini 1-Oct-22 16:07pm    
Wow. 5.
Richard MacCutchan 1-Oct-22 16:28pm    
Thanks, more than 30 years since I wrote 8080 ASM, so I'm surprised I can still remember it. But don't ask me where I left my glasses. :(
Member 15784866 2-Oct-22 3:48am    
hI Richard" many thanks! I'm having another problem, I just corrected the code but the debugger says that there operands don't match in the "MOV NUM1, AX" instruction..it's a 8 bit address and a 16 bit register..can we correct this point? I must excuse me, I should have brush up the basics but I was in hurry and I started with the practice immediately..;,
Richard MacCutchan 2-Oct-22 4:05am    
You need to change your memory variables to integer types, not character, so use DW in the declarations:
NUM1 DW ?
NUM2 DW ?
SOMMA DW ?

That will allow for values greater than 9.
CPallini 2-Oct-22 15:46pm    
:thumbsup:
Quote:
How can I correct this code?

Not a direct solutin, but advices:
- use comments in code, it ease others to understand what you intend to do. And 1 year, it will ease your understanding of your own code.
- Use debugger, it will show you what your code is really doing. It is a great learning tool as it show you step by step what your code is really doing.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900