20个包含所有大写字母的随机字符串 [英] 20 Random Strings containing all capital letters

查看:329
本文介绍了20个包含所有大写字母的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个过程,该过程生成长度为L的随机字符串,其中包含所有大写字母.调用过程时,我需要在EAX中传递L的值,并传递一个指向将保存随机字符串的字节数组的指针.然后,我需要编写一个测试程序,该程序调用您的过程20次,并在控制台窗口中显示字符串.

I need to create a procedure that generates a random string of length L, containing all capital letters. When calling the procedure, I need to pass the value of L in EAX, and pass a pointer to an array of byte that will hold the random string. Then I need to write a test program that calls your procedure 20 times and displays the strings in the console window.

下面的代码无法正常工作,并出现以下错误:

The code below wont work it comes back with these errors:

Line (33): error A2008: syntax error : main ENDP
Line (35): error A2144: cannot nest procedures
Line (46): error A2008: syntax error : RandomString
Line (48): error A2144: cannot nest procedures
Line (59): warning A6001: no return from procedure
Line (66): fatal error A1010: unmatched block nesting

我对汇编语言还是很陌生...关于我做错了什么以及如何解决这些错误的任何想法?谢谢.

I am still very new with Assembly Language...Any ideas on what I'm doing wrong and how to fix these errors? Thank you.

;Random Strings.

INCLUDE Irvine32.inc
TAB = 9                       ;ASCII code for Tab
strLen=10                     ;length of the string

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
str1 BYTE"The 20 random strings are:", 0
arr1 BYTE strLen DUP(?)

.code
main PROC
     mov ed x, OFFSET str1         ;"The c20 random strings are:"
     call WriteString              ;Writes string
     call Crlf                     ;Writes an end-of-line sequence to the console window.
     mov ecx,20                    ;Create 20 strings

 L1: mov esi,OFFSET arr1           ;ESI: array address
     mov eax,strLen                ;EAX: string length
     call RandomString             ;generates the random string
     call Display
     mov al,TAB
     call WriteChar                ;leaves a tab space
     exit
main ENDP

     RandomString PROC USES eax esi
     mov ecx,eax                   ;ECX = string length

L1:  mov eax, 26
     call RandomRange
     add eax,65                    ;EAX gets ASCII value of a capital letter
     mov arr1[esi],eax
     inc esi

     loop L1

     RandomString EXDP

     Display PROC USES eax esi     ;Displays the generated random string

     mov ecx,eax                   ;ECX=string length

L1:  mov eax, arr1[esi]            ;EAX = ASCII value
     call WriteChar                ;writes the letter

     inc esi

     loop L1

     Display ENDP


        call dumpregs

        INVOKE ExitProcess,0

END main

推荐答案

  • ENDP指令不带标签.而且没有错别字.您的行不通,因此下一个PROC指令将在过程内部打开嵌套过程,这在MASM中是不合法的.

    • 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.

      mov arr1[esi],eax存储4个字节,而不是一个字节(请考虑一下,当您使用最后3个字母并且缓冲区只有10个字节长时会发生什么情况.)

      mov arr1[esi],eax stores 4 bytes, not one (consider what happens when you are at last 3 letters and buffer is only 10 bytes long).

      ENDP只是MASM指令,不是指令,因此您的RandomString代码将在loop指令后继续执行某事,无论随后的内存中发生了什么.您可能对 ret指令感兴趣.并检查Display子例程.

      The ENDP is only MASM directive, not instruction, so your RandomString code will continue executing something after the loop instruction, whatever happens to be in the following memory. You probably may be interested into ret instruction. And check also Display subroutine.

      RandomString使用esi作为输入参数,您可以在其中设置目标缓冲区的地址.然后执行mov arr1[esi],...,因此将执行arr1+arr1地址计算,很可能导致无效的内存访问(或静默内存覆盖某处,但绝对不在您的缓冲区中). mov [esi],...就足够了,如果它已经包含指向缓冲区的指针.

      RandomString uses esi as input argument, where you set the address of target buffer. Then it does mov arr1[esi],..., so it will do arr1+arr1 address calculation, resulting very likely into invalid memory access (or silent memory overwrite somewhere, but definitely not in your buffer). mov [esi],... is enough, if it already contains pointer to buffer.

      Display使用esi,但是您没有将其设置在call Display之前,因此无论RandomString放在哪里,它都可以在esi中找到.然后再次执行arr1[esi],即arr1+esi地址计算.

      Display uses esi, but you don't set it ahead of call Display, so it will find in esi whatever the RandomString left there. And it does again arr1[esi], i.e. arr1+esi address calculation.

      "EAX = ASCII值" ...我非常怀疑,因为ASCII值仅需要8位,并且您从内存中加载了32位.此时的eax很可能包含4个字符.但是WriteChar将仅使用eax的低8位,因此它可以按预期工作,但这仍然是一个bug,表明您对本机CPU数据类型/寄存器的误解/无知.

      "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.

      有多个L1标签,我认为它们在MASM中是全局的(但也许PROC会将它们本地化).总的来说,如果能告诉您我在代码标签L1中,它对您有什么作用?如果我将其重命名为AlienInvasionV4_HandlerOfMultidimensionalTeleportationError:,怎么办?即使不看代码,您还能猜出它的功能吗?使用诸如"L1"之类的神秘东西有什么意义?

      there are multiple L1 labels, I thought they are global in MASM (but maybe PROC will localize them). Overall if will tell you I have in my code label L1, what does it say to you about what is it used for? How about if I renamed it to AlienInvasionV4_HandlerOfMultidimensionalTeleportationError:, can you guess anything about its function even without seeing the code? What the point in using something cryptic like "L1"?

      main中没有循环.

      RandomString将更改ecx(即使main中存在循环,也无法按预期工作).

      RandomString will change ecx (even if there would be loop in main, it will not work as expected).

      Display也会尝试使用eax值作为输入,但是您不要在call Display之前设置它.

      Display will try to use also eax value as input, but you don't set it ahead of call Display.

      ...可能还有更多错误,但是我厌倦了通读它(我没有Windows + irvine lib来实际运行它,所以我的所有笔记只是通过校对您的源代码并在头运行而实现的. ..想像一下您也可以自己读一遍,然后对每条指令进行推理,哇!)...您应该已经能够自己找到并修复其中的大部分内容,不确定您在此处提出的要求是什么.与修复错误地使用ENDP的一些语法错误相比,它要乏味得多.尽管您的代码具有算法上的精明之处,但是却一点也不了解,就像您根本不了解CPU(除非您错过了寄存器的超级全局"性质,并期望它们在调用时保留其值)一样,更像是缺乏的精度和经验.使用ASM时,您将需要更高的精度,该机器将愉快地执行您向其抛出的任何法律指令,而不会对后果发出任何警告.

      ... maybe some more bugs, but I got tired of reading through it (I don't have windows+irvine lib to actually run it, so all my notes are just by proofreading your source and running it in head... imagine you could read it after yourself too and reason about each instruction, whoa!) ... you should have been able to find+fix most of these on your own, not sure what you are asking here on SO. It will get lot more tedious than fixing few syntax errors with wrongly used ENDP. Although your code has glimpse of algorithmic sanity, doesn't feel completely clueless like you don't understand CPU at all (except you missed the "super global" nature of registers and expect them to keep their values over calls), more like lack of precision and experience. You will need lot more precision with ASM, the machine will happily execute any legal instruction you throw at it, without any warning about consequences.

      这篇关于20个包含所有大写字母的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆