Search code examples
assemblyemu8086

How to print a message dynamically in EMU8086


This my code;

org 100h

mov cx,5
loop1:  
        call DISPLAY
        dec cx
        cmp cx,0
        ja loop1
        jmp Exit

DISPLAY proc
 MOV AH,09
 MOV DX, offset SCREEN
 INT 21h
 RET
DISPLAY ENDP         

Exit:
ret

SCREEN DB 'Number 1','$'  

This code is gonna display five times 'Number1' but I want to print the screen in the following way;

Number 1  
Number 2  
Number 3  
Number 4  
Number 5  

How do I do this??
Thank you everyone!!!


Solution

  • You can concatenate the char value of line number with your main message :

    org 100h
    
    mov cx,5
    loop1:  
            call DISPLAY
            dec cx
            cmp cx,0
            ja loop1
            jmp Exit
    
    DISPLAY proc
     MOV AH,09
     MOV BX,  offset SCREEN 
     MOV AL,CL
     NEG AL
     ADD AL,'0'+6
     MOV [BX+7],AL  
     MOV DX,BX
     INT 21h
     RET
    DISPLAY ENDP         
    
    Exit:
    ret
    
    SCREEN DB 'Number  ',0x0A,0x0D,'$'
    


    Edited:

    This is a more general form that is like printf function and support %d and %s(Although this code may be unsafe!):

    org 100h
    mov cx,LINES_NUMBER
    xor ax,ax 
    loop_main:
        inc ax
        mov offset WORD_ARG,ax
        pusha
        push offset STR_ARG
        push offset WORD_ARG
        mov si,offset TEXT_FORMAT
        mov di,offset OUT_BUFF
        call sprintf
        mov ah,0x09  
        mov dx,offset OUT_BUFF
        INT 21h
        popa
    loop  loop_main
    ret
    end:
    
    
    ; sprintf
    ; si : Source String
    ; di : Destination String
    ; You can use %d and %s
    sprintf proc
        pop cx
        start_proc:      
            mov ah,[si]
            cmp ah,'%'
            je is_arg 
            cmp ah,'$'
            je exit_proc    
            mov ah,[si]
            mov [di],ah
            inc si
            inc di
            jmp start_proc
        is_arg:
            inc si
            mov ah,[si]
            cmp ah,'d'
            je add_decimal
            cmp ah,'s'
            je add_string
            mov [di],'!'
            inc di
            jmp exit_proc
            jmp start_proc
    
        add_decimal:
            inc si
            pop bx
            mov ax,[bx]
            mov dx,0x8000
            and dx,ax 
            je not_neg
            mov [di],'-'
            inc di
            neg ax
        not_neg:
            push ax
            mov bx,10 
            push cx
            xor cx,cx     
        ad_count_digits:
            xor dx,dx
            inc cx
            div bx
            test ax,ax
            jne ad_count_digits
            dec cx
            mov bx,cx
            pop cx
            pop ax
            xor bh,bh
            push bx
            add di,bx
            mov bx,10
        ad_print_digits:
            xor dx,dx
            div bx
            add dl,'0'
            mov [di],dl
            dec di
            test ax,ax
            jne ad_print_digits
            xor bx,bx
            pop bx
            add di,bx
            add di,2
            jmp start_proc   
        add_string:
            inc si
            pop bx
        insert_string:
            mov ah,[bx]
            mov [di],ah
            inc bx
            inc di        
            cmp ah,'$'
            jne insert_string
            dec di
            jmp start_proc     
        exit_proc:
            mov [di],'$'    
        push cx          
        ret
    sprintf endp
    
    
    LINES_NUMBER EQU  5
    WORD_ARG DW 0
    STR_ARG DB 'People.',0x0A,0x0D,'$'
    TEXT_FORMAT DB 'Number %d %s$'
    OUT_BUFF DB 0 DUP(32)