I typed in Chapter 5.8, Problem 6PE but it does not work. Can you help me? TITLE
ID: 3918469 • Letter: I
Question
I typed in Chapter 5.8, Problem 6PE but it does not work. Can you help me?
TITLE Random Strings Program (ranStr.asm)
;Ch5 Q6
INCLUDE Irvine32.inc
TAB = 9;
.data
str1 BYTE "The 20 random strings are: ",0
.code
main PROC
call Clrscr
mov ed x, OFFSET str1
call WriteString
Call Crlf
mov ecx,20
Label1:
call RandomString
mov al, TAB
call WriteChar
loop Label1
exit
main ENDP
RandomString PROC
mov ecx,10
Label1:
mov eax,26
call RandomRange
add eax,65
call WriteChar
loop Label1
RandomString ENDP
END main
Explanation / Answer
Considering your program is in "Assembly Language for x86 Processors" as no specification is mentioned in question.
I tried to code in Assembly Language for x86 Processors.
First.
There can be various problems:
->The ENDP directive goes without label. And without typos. Yours don't work, so the next PROC directive opens nested procedure inside procedure, that's not legal in MASM.
->"EAX = ASCII value" ... I would highly doubt that, as ASCII values need only 8 bits, and you load 32 bits from memory. The eax at that point will very likely contain 4 characters. But WriteChar will use only the bottom 8 bits of eax, so it will work as expected, but it's still sort of bug, showing your misunderstanding/ignorance of native CPU data types/registers.
These were major bugs. Maybe there are some more minor ones.
I have tried the code below which works fine on my system. Refer it:
Code:
INCLUDE Irvine32.inc
strLen=10
.data
arr BYTE strLen DUP(?)
.code
main PROC
call Clrscr
mov esi, offset arr
mov ecx, 20
L1:
call GenerateRandomString
Loop L1
call WaitMsg
exit
main ENDP
GenerateRandomString PROC USES ecx
mov ecx, lengthOf arr
L2:
mov eax, 26
call RandomRange
add eax, 65
mov [esi], eax
call WriteChar ; write character
loop L2
call Crlf
ret
GenerateRandomString ENDP
END main