Search code examples
assemblyx86irvine32

Prompting for user input, counting string length and reprinting input


I am working on an assignment and stuck. I need to prompt the user to enter a sentence using WriteString and store the input using ReadString. Additionally, I need to count the length of the string and then reprint the users input. I dont want the code, just a push in the right direction. I have no problem getting my program to prompt and take the user input, but I'm looking for help counting the length of the string in decimal.

.data
String1 BYTE "Enter a sentence", 0
String2 BYTE 50 DUP (?)
String3 BYTE "Decimal length: ", 0
String4 BYTE "Hexidecimal length: ", 0
String5 BYTE "You entered: ", 0

.code
main proc
Exercise1 PROC

mov edx, OFFSET String1 ; move String1 to edx
call WriteString ;prompt user to enter their sentence
mov edx, OFFSET String2 ;array to store input
call ReadString ;store input to edx

;mov ecx, 20
;mov dl, 0 ;use to count length
;how do I loop through String2 to determine its length? 

Exercise1 ENDP

Solution

  • You don't need to count the length, ReadString returns the number of bytes read in EAX. http://programming.msjc.edu/asm/help/source/irvinelib/readstring.htm

    (Also, you have to pass it a max read size in ECX, e.g. mov ecx, 50-1 before the call. ReadString needs to know how big your buffer is, so it won't overflow.)


    If you want to search for the first 0 byte in the string yourself, google x86 assembly strlen and take any of the zillions of hits. Or search here on SO.