Search code examples
assemblymasmirvine32

Array of underscore


So in this code, I'm trying to insert _ to an array but I don't really know what's wrong with this code. Because it seems to get stucked when the _ is being inserted to the array. Is there better way to implement or fix the code?

.data
strUnderscore BYTE 20 DUP (?)             ;the array
.code
mov ECX,stringLength 
mov ESI, OFFSET strUnderscore 
L1: 
mov strUnderscore[ESI], '_'            ;the code runs but stucks here
inc ESI 
loop L1

mov EDX, OFFSET strUnderscore 
call WriteString

Solution

  • Before the loop starts, ESI already points at strUnderscore, because you intialize it that way. Meanwhile, the line

    mov strUnderscore[ESI], '_'
    

    tries to write a character to address strUnderscore + ESI. That's not right. In C-like terms, what you have now is:

    char *esi = strUnderscore;
    while(...)
    {
        strUnderscore[(int)esi] = '_';
        esi++;
    }
    

    You want either a running index, or a running pointer. Either initialize ESI to 0 (and it will be used as an index), or change the destination address in the mov command to byte ptr [ESI].

    Also, LOOP is a bad command (look it up). Consider a decrement and a conditional jump instead.