How do I use ReadString in Assembly?

Jon picture Jon · Nov 27, 2014 · Viewed 15k times · Source
mov  edx,offset Prompt1     
call WriteString

mov  ecx,32     
mov  edx,offset String1     
call ReadString

Now, how do I go about accessing String1? How do I move it into a register so I can do shifting operations on it?

For example,

mov eax, edx
shr eax, 1

The problem that I'm having is that I can't figure out how to access String1. It doesn't seem to be going into the eax register, but if I call WriteString it will appear, so I believe it is in EDX.

Answer

Jim Mischel picture Jim Mischel · Nov 27, 2014

The data is read into memory starting at the address of String1. There is a null byte (0) after the last input character.

So if after your call ReadString, you write mov edx,offset String1, then EDX is pointing to the first character of the string.

You can then process the string. For example, to add 1 to each character:

    call ReadString
    mov edx,offset String1
theLoop:
    mov al,[edx]
    cmp al,0
    jz done    ; the character is 0, so done
    inc al
    mov [edx],al
    inc edx    ; next character
    jmp theLoop
done:

So if the input was "0123abc", that would change the string to "1234bdc".

(for the nitpickers: Yes, I know this could be optimized. In particular the cmp al,0. But it's better for beginners to think of comparing ... we can work on optimizations after they have a firm grasp of working at the processor level.)