1

When I add 3+3 its answer is correct but when I add 7+7 it's not working. And I want to add two numbers like 75+75 its answer should be 150 or 900+100 its answer should be 1000.

What is its procedure please tell me. What is the wrong with my code,I'm sorry cause I'm just new assembly language..

.model small
.stack 0100h
.data
     num1 db ?
     num2 db ?
     msg1 db 13,10, "Enter 1st Number : $"
     msg2 db 13,10, "Enter 2nd Number : $"
     msg3 db 13,10, "The answer is : $"
.code

    mov ax, @data
    mov ds, ax

    lea dx,msg1
    mov ah,09h
    int 21h

start:  
    mov ah,01h
    int 21h
    cmp al,0dh              
    je second                                                           
    sub al,30h                                                            
    push ax                                                            
    mov num1,al 
    jmp start

second:
    lea dx,msg2
    mov ah,09h
    int 21h

number2:    
    mov ah,01h
    int 21h
    cmp al,0dh              
    je ans                                                          
    sub al,30h                                                            
    push ax                                                            
    mov num2,al 
    jmp number2

ans:

    mov al,num1
    add al,num2

    MOV AH,9
    LEA DX,MSG3 
    INT 21H

    ADD AL,30H
    MOV AH,2h
    MOV DL,AL
    INT 21H

jmp start

end 
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 3
    This is the same ol' problem: multi-digit numbers. Look how to convert a string into a number (atoi) and the reverse (itoa). There should be plenty of questions on that. – Margaret Bloom Dec 06 '16 at 08:57
  • i know how to reverse but ,I don't know if how to solve that problem right now.. is there something that you can help me to fix my code?? thanks – Gerryl L Destor Dec 06 '16 at 09:17
  • 1
    Margaret just gave you the solution. You are trying to convert a multi-digit string representing a number into a number. There is *no* panacea instruction to do that but, instead, the need to loop through the string reading right to left and converting from text to integer and multiplying. There are a great many examples in assembly for `atoi` to google. – Frank C. Dec 06 '16 at 09:34
  • What is "string" in Assembly: http://stackoverflow.com/a/40580889/4271923 How to convert (unsigned) numeric value into string: http://stackoverflow.com/a/40505938/4271923 .. String to value: http://stackoverflow.com/questions/10471281/how-to-convert-string-to-number-in-tasm ... and you should also learn what is `byte/word/dword`, and what amount of information they can handle, as `num1 db ?` is not enough to handle numbers of 0-900 range, so your example of 900+100 would fail anyway. You are `push`ing `ax` on the stack, but not using it. That's bad practice, almost harmless in this case, but.. – Ped7g Dec 06 '16 at 10:11
  • how to use the ax that i push ??... i'm very sorry cause i'm just a newbie in this assembly programming.. – Gerryl L Destor Dec 06 '16 at 11:50
  • @GerrylLDestor so why do you `push`, if you don't know, what it does? Just keep learning, going through the class materials, online tutorials and Intel documentation. Everything is explained there, and as the CPU instructions do only very simple deterministic changes to the CPU state, it's actually very simple to read what particular instruction does. The hard part is to understand how that tiny state change works together with other ones, and how they form complex algorithms. – Ped7g Dec 06 '16 at 13:27

1 Answers1

0

simple answer: by using ADD <16bit Register>,<16bit Register>

you're confusing two things: binary representation and the ASCII representation. Your cpu doesnt know what "4711" or "12345" means. It's just a series of bytes.

you need to convert the ASCII-representation of your strings, add their binary values, and convert the result back to ASCII

for both, there are a lot of articles in SO

Tommylee2k
  • 2,683
  • 1
  • 9
  • 22
  • Or he *can* actually add two ASCII strings digit by digit... sometimes when I see these student questions, I'm tempted to post solution done this way, just to try out how the lector will react... :D – Ped7g Dec 06 '16 at 13:25