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

1 Answer 1

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.

10
  • Unfortunately, the problem still exists after apply your code.
    – Lion King
    Apr 2, 2012 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 2, 2012 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 2, 2012 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 2, 2012 at 12:28
  • @Lion: okay, first things first. What do you want to output? You haven't yet told us that.
    – paxdiablo
    Apr 2, 2012 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.