Home > Software engineering >  TASM Printing a number array
TASM Printing a number array

Time:11-09

I am trying to get ahead of my uni courses and got learning assembler on my own, so I am a little bit stuck.

I have this practice problem and I don't understand exactly how to print correctly.

I am given 3 values as digits: var1, var2, var3. For example: var1 = 4, var2 = 5 and var3 = 6. I am also given an array nstr db 7 dup(' ').

What I need to do is add var1 var2 var3 to nstr[5], var1 var2 to nstr[4] and var1 to nstr[3] and print the new array.

.model small
.STACK 100h
.data
    var1 db 4
    var2 db 5
    var3 db 6
    nstr db 7 dup(' ')  
.code
.startup
                            
    mov ax, dgroup
    mov ds, ax
                            
    mov si,6
    mov nstr[si], '$'           
    dec si
    
    mov al, var1
    add al, var2
    add al, var3
    
    mov nstr[si], al
    dec si
    
    sub al, var3
    mov nstr[si], al
    dec si
    
    sub al, var2
    mov nstr[si], al

print:
    mov ah, 09h
    mov dx, offset nstr
    int 21h 

stop:
    mov ah, 4ch
    int 21h
end

Every time i try to print it, the console displays a symbol instead of the numbers. I understand that I need to convert the numbers to strings before adding them into the array.

The only method I have seen is to div by 10 and loop over the number until there is no characters left in it and add every character to the array.

My problem is that I need to have a two digit number on a single array position, in this example nstr[5] needs to be 15, using the method above I would get nstr[2] = 4, nstr[3] = 9, nstr[4] = 1 and nstr[5] = 5, which isn't the result I need, even if the printing is correct.

Is there any way to accomplish this deed? Thank you!

CodePudding user response:

Every time i try to print it, the console displays a symbol instead of the numbers. I understand that I need to convert the numbers to strings before adding them into the array.

This begs the question: why didn't you make the conversion?
For nstr[3] and nstr[4] a mere addition with 48 would have turned the number into a printable character.

The only method I have seen is to div by 10 and loop over the number until there is no characters left in it and add every character to the array.

There's more than decimal that you can use. If you select hexadecimal the task can be solved.

.data
    var1 db 4
    var2 db 5
    var3 db 6
    nstr db 7 dup(' ')
    hexa db "0123456789ABCDEF"
.code
.startup                     
    mov ax, dgroup
    mov ds, ax
    mov bx, offset hexa                
    mov si, 6
    mov nstr[si], '$'           
    dec si
    
    mov al, var1
    add al, var2
    add al, var3
    xlatb
    mov nstr[si], al
    dec si

    mov al, var1
    add al, var2
    xlatb
    mov nstr[si], al
    dec si
    
    mov al, var1
    xlatb
    mov nstr[si], al
    ...

The above should print:

   49F

Alternatively, use a loop to print the 7 array elements for what they are: byte-sized numbers:

32, 32, 32, 4, 9, 15, 32

The fact that the nstr array was defined nstr db 7 dup(' '), using a space character, does not necessarily entail that you have to use it as a character string. Ultimately dup reserves space and nothing else.

  • Related