0

I want to do something simple in assembly language.
addition two numbers, and print the result on the screen.

I did that code:

.Model SMALL
.Stack 100h

.Code
start:
   MOV ax, 10
   ADD ax, 5
   MOV ah, 02h
   INT 21h 

   MOV ah, 01h
   INT 21h

   MOV ah, 4ch
   INT 21h

end start

After compile the code without any error, tells me a strange character .


Modified:

MOV dl, 10
ADD al,5
MOV dl, al

MOV ah,02h
INT 21h 

but still print a strange character I don't know what can I do to print number on the screen

Lion King
  • 32,851
  • 25
  • 81
  • 143

1 Answers1

2

Yes, you will most likely get a strange character because int 21/ah=02 requires the character to print to be in the dlregister, and you haven't populated dl with anything.

You may want to transfer the value with something like:

mov  ax, 10
add  ax, 5

push ax             ; these are the two new lines.
pop  dx

mov  ah, 02h

However, keep in mind that, even if you do transfer the value from al to dl, character number 15 may not be what you expect. 15 is one of the ASCII control characters and I'm not sure what the DOS interrupts will output for them.

If you want to print out the digits15, you will need two calls, one with dl = 31h and the second with dl = 35h (the two ASCII codes for the 1 and 5 characters).

If you want to know how to take a number in a register and output the digits for that number in readable form, there's some pseudo-code in an earlier answer of mine.

From that answer, you have the pseudo-code:

    val = 247

    units = val
    tens = 0
    hundreds = 0
loop1:
    if units < 100 goto loop2
    units = units - 100
    hundreds = hundreds + 1
    goto loop1
loop2:
    if units < 10 goto done
    units = units - 10
    tens = tens + 1
    goto loop2
done:
    if hundreds > 0:                 # Don't print leading zeroes.
        output hundreds
    if hundreds > 0 or tens > 0:
        output tens
    output units
    ;; hundreds = 2, tens = 4, units = 7.

Now you need to translate that into x86 assembly. Let's start with the desired value in ax:

    mov  ax, 247                 ; or whatever (must be < 1000)
    push ax                      ; save it
    push dx                      ; save dx since we use it

    mov  dx, 0                   ; count of hundreds
loop1:
    cmp  ax, 100                 ; loop until no more hundreds
    jl   fin1a
    inc  dx
    sub  ax, 100
    jmp  loop1
fin1a:
    add  dx, 30h                 ; convert to character in dl
    push ax                      ; save
    mov  ah, 2
    int  21h                     ; print character
    pop  ax                      ; restore value

    ; now do tens and units the same way.

    pop dx                       ; restore registers
    pop ax

Now that code segment (notwithstanding any errors due to the fact it's been a while since I did assembly) should print out the hundreds digit and leave ax with only the tens and units digit.

It should be a simple matter to duplicate the functionality twice more to get the tens and units places.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Unfortunately, the problem still exists after apply your code. – Lion King Apr 02 '12 at 12:00
  • @Lion, see the paragraph starting "However, keep in mind ...". _What_ do you actually want to print? If it's the _string_ 15, I've added a final paragraph telling you what you need to do. – paxdiablo Apr 02 '12 at 12:11
  • 3
    No. I've given you more than enough to be able to acheive what you want, with a bit of effort from yourself. SO is not a "give me the codez" site. If you want some lackey to write your code for you, I suggest you go look at elance or rentacoder and pay for it. – paxdiablo Apr 02 '12 at 12:24
  • First I thank you for continue your help me, and I don't say I want a lackey to write my code, but I do not understand any piece of information unless only with the example – Lion King Apr 02 '12 at 12:28
  • @Lion: okay, first things first. _What_ do you want to output? You haven't yet told us that. – paxdiablo Apr 02 '12 at 12:31
  • simply,I want output the result like (A = A + B); print A – Lion King Apr 02 '12 at 12:33
  • @Lion, this latest update is about all I can give you without doing the whole lot myself, and that benefits neither you nor me. – paxdiablo Apr 02 '12 at 12:51
  • :For the second time I thank you for your help, and I'm sorry for fatigue – Lion King Apr 02 '12 at 12:55
  • No probs, if you get that code in and have a problem with it, ask another question, and I (or someone else) will help out again. – paxdiablo Apr 02 '12 at 12:59
  • I was given your answer is the best answer, not because your code is correct only, but because you still trying help me. thank you – Lion King Apr 02 '12 at 17:11