Search code examples
arraysassemblyemu8086

print elements of array in assembly


I am new in assembly , I am using emu8086

I was trying to print two elements of an array , but I could not print the second element

Here is my code:

.MODEL SMALL

.STACK 100H  

.DATA 
MSG DB 'HI','GOOD$'

.CODE 

MAIN PROC

MOV AX,@DATA
MOV DS,AX
 
MOV AH,2
MOV DL,MSG
INT 21H  

MOV AH,2
MOV DL,MSG+1
INT 21H 

MOV AH,4CH
INT 21H

MAIN ENDP

END MAIN

at output hi is printed , good is not printed . please correct me how can print the second element.


Solution

  • If all you want to do is print "HIGOOD" then write:

    MOV AH,2
    MOV DL,MSG
    INT 21H  
    MOV DL,MSG+1
    INT 21H
    MOV DL,MSG+2
    INT 21H  
    MOV DL,MSG+3
    INT 21H
    MOV DL,MSG+4
    INT 21H  
    MOV DL,MSG+5
    INT 21H
    

    A far better approach is to "$"-terminate both strings like MSG DB 'HI$','GOOD$' and then use the string output function 09h:

    MSG DB 'HI$','GOOD$'
    ...
    mov ah, 09h
    mov dx, offset MSG
    int 21h
    mov dx, offset MSG+3
    int 21h
    

    Even better is to assign separate labels to your strings:

    MSG1 DB 'HI$'
    MSG2 DB 'GOOD$'
    ...
    mov ah, 09h
    mov dx, offset MSG1
    int 21h
    mov dx, offset MSG2
    int 21h