Search code examples
arraysassemblyx86masmirvine32

Concatenating a string and the size of an array in Assembly x86 MASM


I was able to loop through the array and print out the values. However, I also wanted to print out the string "The length of my array is 7", with 7 being the length of (the number of elements) in the array. However, I cannot concatenate the string with the length of the arrays. Please help. Thank you.

INCLUDE Irvine32.inc 
.data
    myarray byte 23, 2, 3, 40, 5, 16, 7
    x byte 5 
    l dword lengthof myarray 
    msg1 byte "The length of my array is ",0
    msg2 byte "-------------------------------",0
    i byte 0
    .code 
    main PROC 

        mov eax, 0
        mov esi, offset myarray;
        mov ecx, l 



        myloop:
        mov al, [esi]
        call writedec 
        call crlf 
        inc esi 
        mov edx, OFFSET msg1 
        mov edx, l

        loop myloop

        call writestring
        call crlf 
        call crlf

        exit 

        main ENDP
        end main

The result I am getting is the following:

23
2
3
40
5
16
7

"esimovarray.asm has stopped working"

Please help. Thank you.


Solution

  • I think we only need to change the order of a few lines of code:

    INCLUDE Irvine32.inc 
    .data
        myarray byte 23, 2, 3, 40, 5, 16, 7
        x byte 5 
        l dword lengthof myarray 
        msg1 byte "The length of my array is ",0
        msg2 byte "-------------------------------",0
        i byte 0
        .code 
        main PROC 
    
            mov eax, 0
            mov esi, offset myarray;
            mov ecx, l 
    
            myloop:
            mov al, [esi]
            call writedec 
            call crlf 
            inc esi 
            ;mov edx, OFFSET msg1         ;◄■■■ NOT HERE.
            ;mov edx, l                   ;◄■■■ NOT HERE.
            loop myloop
    
            mov edx, OFFSET msg1          ;◄■■■ RIGHT HERE!
            call writestring
    
            mov eax, l                    ;◄■■■ RIGHT HERE! MUST BE EAX.
            call writedec
    
            exit 
    
            main ENDP
            end main