1

So I have a string where I want to add more to it according to user input. For example, the string's default is "The two numbers from input are: $" , and once the user inputs 2 numbers, lets say 21 and 42, then the String should change to "The two numbers from input are: 21 42" so that I can write them into a file. Here is my code:

.model small          
.stack 100
.data 
        Num1 DB ?     ;input  
        Num2 DB ?

        text db "The two numbers from input are:  $"  ;This is the string I want to change according to input
        filename db "decimal.txt",0
        handler dw ?    


.code
START:          
  mov  ax,@data
  mov  ds,ax 

;////////////////////////////        
        ;INPUT1
        ;Get tens digit
        MOV ah, 1h   
        INT 21h      
        SUB al,30h   
        MOV dl, al                     

        ;Multiply first digit by 10 (tens)  
        MOV cl, al
        MOV ch, 0
        MOV bl, 10
        MUL bl   
        MOV Num1, al 

        ;Get ones digit    
        MOV ah, 1h  
        INT 21h     
        SUB al,30h   
        MOV dl, al   
        ADD Num1, dl


        ;INPUT2
        ;Get tens digit
        MOV ah, 1h   
        INT 21h      
        SUB al,30h   
        MOV dl, al                     

        ;Multiply first digit by 10 (tens)  
        MOV cl, al
        MOV ch, 0
        MOV bl, 10
        MUL bl   
        MOV Num2, al 

        ;Get ones digit    
        MOV ah, 1h  
        INT 21h     
        SUB al,30h   
        MOV dl, al   
        ADD Num2, dl 

;//////////////////////////        

        ;FILE
        ;create file
        MOV  ah, 3ch
        MOV  cx, 0
        MOV  dx, offset filename
        INT  21h  

        ;file handler
        MOV handler, Ax

        ;write string
        MOV ah, 40h
        MOV Bx, handler
        MOV Cx, 50  ;string length
        MOV Dx, offset text
        INT 21h  

        MOV Ax, 4c00h
        INT 21h

    end start

Is there any way to do it? I tried to search on the internet but all I keep getting is concatination of two strings, which wasn't useful because I don't know how to change my input into string.

Tess
  • 41
  • 1
  • 7

1 Answers1

2

In general, you cannot directly append a number to a string in assembler. What you have to do is to convert the number to a string first and then concatenate the two strings.

However, that being said, it is slightly different in your use case. Since you already know that both your numbers will have two digits, you can do something like string formating (à la printf). That is: you already reserve some space in your string for where the numbers should go and write the digits to that location.

Furthermore, you are obviously reading the numbers as characters from the input. You could therefore directly store these characters into your string, before you convert them to their actual numeric values.

I only wrote down the parts of the code that you need in order to complete your own implementation:

.data
    ; note the 'NN' as placeholder for the numbers to follow
    text  db  "My numbers: NN NN", $

.code
START:
    ...
    mov   ah, 01h           ; read a character
    int   21h

    mov   ds:[text+13], al  ; store the character at position 13 in the text

    sub   al, '0'           ; convert the character to a number as before
    mov   cl, al
    ...
Tobias
  • 1,321
  • 6
  • 11
  • 1
    `ds:[dx]` is not a [valid 16-bit addressing mode](https://stackoverflow.com/questions/12474010/nasm-x86-16-bit-addressing-modes). Also, don't use a runtime `add` instruction when you could have done `mov [di+13], al`, or `mov di, OFFSET text+13`, or even just `mov [text+13], al` – Peter Cordes May 20 '20 at 18:33
  • Also, `sub al, '0'` is more human-friendly than the magic constant `30h` as the ASCII code for `'0'`. – Peter Cordes May 20 '20 at 18:35
  • @Peter You are right. Apologies, it is more than a decade since I last wrote some 16-bit assembler. I will correct it accordingly. – Tobias May 20 '20 at 18:57
  • I wish I didn't have part of my brain wasting space on obsolete 16-bit crap, but people keep posting questions about it on Stack Overflow. :/ Either for legacy BIOS MBR toy "operating system" hobby projects, or some schools teach assembly using 16-bit x86 with emu8086 or DOSBox + some assembler. As if segmentation was still relevant to anything ever. – Peter Cordes May 20 '20 at 19:06