Search code examples
assemblyx86-16emu8086

How do I append to a string?


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.


Solution

  • 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
        ...