Search code examples
stringassemblyasciidosx86-16

ASCII string to asciiz (assembly 8086)


I'm working with some folder interrupts in assembly 8086 language and I need to read an ascii string and convert it in an asciz one in order to specify some parameters. -How can I add the null character at the end of the string? -If I read the string with ah=9 int 21h does it add the carriage return character? I really need help! Thanks for the attention.


Solution

  • Strings are captured from keyboard with interrupt 21h service 0AH (09H is for displaying). The captured string ends with chr(13). To display this string it's necessary to replace chr(13) by '$', or, to use the string for a filename (for example, to create a file and write to it), replace chr(13) by chr(0).

    Capture strings with 0AH requires a variable in data segment with three DB, next image explains the logic :

    enter image description here

    The first two characters are for control (0AH needs them). The useful string starts at position 2 (the third byte).

    To find out where is the ending chr(13), all we have to do is to add the length (the second byte) to the third byte, capisci?

    Now the code. Next little program displays a message to the user and waits for the string, replaces chr(13) by chr(0) (to create and ASCIIZ string), creates a file with the string, writes text to it and closes it. Pay attention to the variable filename in data segment, the three DB :

    .model small
    
    .stack 100h
    
    .data
    
    handle   dw ? 
    
    filename db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
             db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
             db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
    
    msj      db 'ENTER FILE NAME HERE: $' 
    
    text     db 'text for file'  ;LENGTH = 13.
    
    .code
    ;INITIALIZE DATA SEGMENT.
        mov ax, @data
        mov ds, ax
    
    ;DISPLAY MESSAGE.
        mov dx, offset msj
        mov ah, 9
        int 21h      
    
    ;CAPTURE FILENAME FROM KEYBOARD.                                    
        mov ah, 0Ah
        mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
        int 21h                
    
    ;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
    ;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
        mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
        mov cl, [ si ] ;MOVE LENGTH TO CL.
        mov ch, 0      ;CLEAR CH TO USE CX. 
        inc cx         ;TO REACH CHR(13).
        add si, cx     ;NOW SI POINTS TO CHR(13).
        mov al, 0
        mov [ si ], al ;REPLACE CHR(13) BY 0.            
    
    ;CREATE FILE.
        mov ah, 3Ch
        mov cx, 0
        mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
        int 21h
        mov handle, ax  ;PRESERVE FILE HANDLER.
    
    ;WRITE SOME TEXT TO FILE.
        mov ah, 40h          
        mov bx, handle       ;THE FILE.       
        mov dx, offset text 
        mov cx, 13           ;LENGTH OF TEXT.
        int 21h
    
    ;CLOSE FILE
        mov ah, 3Eh   
        mov bx, handle
        int 21h
    
    ;END PROGRAM.
        mov ah, 4ch
        int 21h
    

    Almost forgot : variable filename indicates max length 26 but we expect 25. An extra byte is necessary for chr(13) at the end. For example, if you want to capture 10 chars, specify 11 as max length.