Printing an array of strings in assembly language


I am trying to add strings to an array for later printing from the array and this is what I have. What am I missing?

array dword 20 dup (0)
str1 byte 20 dup (0)
temp dword ?
n dword ?
count dword 0

mes1 db "press 1 to add an element, 2 to print, 3 to quit    ", 0

main PROC

    lea edx, mes1
    call writestring
    call readdec
    cmp eax, 1
    je add1
    cmp eax, 2
    je print2
    cmp eax, 3
    je stop

    call readin
    jmp done

    call print
    jmp done

    jmp start


main ENDP

readin proc
    lea edx, str1
    mov ecx, sizeof str1
    call readstring

    mov ebx, count
    mov eax, [array]
    mov temp, eax
    add temp, ebx
    lea esi, temp
    mov ebx, [str1]
    mov [esi], ebx

readin endp

print proc
    mov esi, 0
    mov ecx, n

        mov eax, Array[esi]
        call writedec
        call crlf
        add esi, 4
        @@while: loop @@do

print endp
END main


The simplest solution is to create an array of bytes that contains null-terminated strings in a row. This is not really an "array of strings". Ingredients: A big buffer and a pointer that points to the end of the valid part of that buffer.



    arrayptr    DWORD OFFSET array
    array       BYTE 4096 DUP (?)

    mes1        BYTE 10, "press 1 to add an element, 2 to print, 3 to quit    ", 0


readin PROC
    mov edx, arrayptr           ; Argument for ReadString: Pointer to memory
    mov ecx, 10                 ; Argument for ReadString: maximal number of chars
    call ReadString             ; Doesn't change EDX
    test eax, eax               ; EAX == 0 (got no string)
    jz done                     ; Yes: don't store a new arrayptr
    lea edx, [edx eax 1]        ; EDX  = EAX   1
    mov arrayptr, edx           ; New pointer, points to the byte where the next string should begin
readin ENDP

print PROC
    lea edx, array              ; Points to the first string

    cmp edx, arrayptr           ; Does it point beyond the strings?
    jae done                    ; Yes -
By : rkhb

This video can help you solving your question :)
By: admin