如何输入包含1到50个字符的字符串。(程序集)

时间:2014-11-10 20:54:14

标签: string assembly masm irvine32 machine-language

;This program reverses a string.
INCLUDE Irvine32.inc
.data
   aName BYTE "Abraham Lincoln",0
   nameSize = ($ - aName) - 1
.code

main PROC
   ; Push the name on the stack.
   mov ecx,nameSize
   mov esi,0
   L1: movzx eax,aName[esi] ; get character
     push eax ; push on stack
     inc esi
   Loop L1
   ; Pop the name from the stack, in reverse,
   ; and store in the aName array.
   mov ecx,nameSize
   mov esi,0
   L2: pop eax ; get character
     mov aName[esi],al ; store in string
     inc esi
   Loop L2
   ; Display the name.
   mov edx,OFFSET aName
   call Writestring
   call Crlf
   exit
   main ENDP
END main

我完成了问题的第一部分,即制作反向字符串程序,但现在我需要修改程序,以便用户可以输入包含1到50个字符的字符串。我不确定如何做到这一点,并想知道是否有人可以提供帮助。这是汇编语言btw

1 个答案:

答案 0 :(得分:2)

您可以使用ReadString from irvine32.lib。因此,您必须将nameSize更改为变量并增加aName的大小。

我为你做了;-):

;This program reverses a string.
INCLUDE Irvine32.inc
.data
   aName BYTE 51 DUP (?)
   nameSize dd ?
.code

main PROC

   mov  edx, OFFSET aName
   mov  ecx, 50            ;buffer size - 1
   call ReadString
   mov nameSize, eax

   ; Push the name on the stack.
   mov ecx,nameSize
   mov esi,0
   L1: movzx eax,aName[esi] ; get character
     push eax ; push on stack
     inc esi
   Loop L1
   ; Pop the name from the stack, in reverse,
   ; and store in the aName array.
   mov ecx,nameSize
   mov esi,0
   L2: pop eax ; get character
     mov aName[esi],al ; store in string
     inc esi
   Loop L2
   ; Display the name.
   mov edx,OFFSET aName
   call Writestring
   call Crlf
   exit
   main ENDP
END main