简体   繁体   中英

Input and print an array of strings in Assembly 8086

I am trying to make a 8086 program in which I input an array of string and then the program prints them. But the program prints only the 1st characters of input strings and jumbled text.

This is the code where I attempted to do it.

data segment
; Definicija podataka
    poruka1 DB "Input array length: $"
    strN DB "        "
    N DW 0
    poruka2 DB "Input string: $"
    strM DB "        "
    niz1 DB 16 dup(?)
ends
; Deficija stek segmenta
stek segment stack
    dw 128 dup(0)
ends
; Ucitavanje znaka bez prikaza i cuvanja     
keypress macro
    push ax
    mov ah, 08
    int 21h
    pop ax
endm
; Isis stringa na ekran
writeString macro s
    push ax
    push dx  
    mov dx, offset s
    mov ah, 09
    int 21h
    pop dx
    pop ax
endm
; Kraj programa           
krajPrograma macro
    mov ax, 4c02h
    int 21h
endm   
           
code segment
; Novi red
novired proc
    push ax
    push bx
    push cx
    push dx
    mov ah,03
    mov bh,0
    int 10h
    inc dh
    mov dl,0
    mov ah,02
    int 10h
    pop dx
    pop cx
    pop bx
    pop ax
    ret
novired endp
; Ucitavanje stringa sa tastature
; Adresa stringa je parametar na steku
readString proc
    push ax
    push bx
    push cx
    push dx
    push si
    mov bp, sp
    mov dx, [bp+12]
    mov bx, dx
    mov ax, [bp+14]
    mov byte [bx] ,al
    mov ah, 0Ah
    int 21h
    mov si, dx     
    mov cl, [si+1] 
    mov ch, 0
kopiraj:
    mov al, [si+2]
    mov [si], al
    inc si
    loop kopiraj     
    mov [si], '$'
    pop si  
    pop dx
    pop cx
    pop bx
    pop ax
    ret 4
readString endp
; Konvertuje string u broj
strtoint proc
    push ax
    push bx
    push cx
    push dx
    push si
    mov bp, sp
    mov bx, [bp+14]
    mov ax, 0
    mov cx, 0
    mov si, 10
petlja1:
    mov cl, [bx]
    cmp cl, '$'
    je kraj1
    mul si
    sub cx, 48
    add ax, cx
    inc bx  
    jmp petlja1
kraj1:
    mov bx, [bp+12] 
    mov [bx], ax 
    pop si  
    pop dx
    pop cx
    pop bx
    pop ax
    ret 4
strtoint endp
; Konvertuje broj u string
inttostr proc
   push ax
   push bx
   push cx
   push dx
   push si
   mov bp, sp
   mov ax, [bp+14] 
   mov dl, '$'
   push dx
   mov si, 10
petlja2:
   mov dx, 0
   div si
   add dx, 48
   push dx
   cmp ax, 0
   jne petlja2
   
   mov bx, [bp+12]
petlja2a:      
   pop dx
   mov [bx], dl
   inc bx
   cmp dl, '$'
   jne petlja2a
   pop si  
   pop dx
   pop cx
   pop bx
   pop ax 
   ret 4
inttostr endp  

start:
    ; postavljanje segmentnih registara       
    ASSUME cs: code, ss:stek
    mov ax, data
    mov ds, ax
    
    ; Mesto za kod studenata
    writeString poruka1
    ; Unos broja elemenata
    push 3
    push offset strN
    call readString
    ; Pretvaranje strN u broj
    push offset strN
    push offset N
    call strtoint
    call novired
    writeString strN
    ; Unos elemenata niza
    mov cx, N
    mov di, 0
    mov si, 0
unos:
    call novired
    writeString poruka2
    push 6
    push offset strM
    call readString
    mov al, strM
    mov niz1[di], al
    add di, 2
    add si, 1
    loop unos
    call novired
    mov cx, N
    mov di, 0
    mov si, 0
ispis:
    writeString niz1[di]
    add di, 2
    add si, 1
    loop ispis
    krajPrograma 
ends
end start

I input an array of string and then the program prints them. But the program prints only the 1st characters of input strings ...

What else could the program print if the 1st character is all that you store in memory?

 mov al, strM mov niz1[di], al add di, 2

... and jumbled text.

  1. With add di, 2 you leave an undetermined byte between any two of these 1st characters. That's not going to look nice. Make that byte the DOS string terminator $ .

  2. Your writeString macro does not load an address but a word from memory. That's not going to work. The invokation writeString niz1[di] gets expanded into mov dx, offset niz1[di] . What you need here is LEA instead of MOV .

Try the following:

    ....
unos:
    call novired
    writeString poruka2
    push 6
    push offset strM
    call readString
    mov  al, strM
    MOV  AH, "$"
    mov  niz1[di], AX
    add  di, 2
    loop unos
    call novired
    mov  cx, N
    mov  di, 0
ispis:
    LEA  DX, niz1[di]
    MOV  AH, 09h
    INT  21h
    add  di, 2
    loop ispis
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM