Search code examples
stringassemblymasmirvine32machine-code

How to input a string containing between 1 and 50 characters.(Assembly)


;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

I finished the first part of the problem which was making a Reverse String program but now I need to modify the program so the user can input a string containing between 1 and 50 characters. Im not to sure how to do this and was wondering if someone could help out. This is in assembly language btw


Solution

  • You can use ReadString from irvine32.lib. Therefor you have to change nameSize to a variable and to increase the size of aName.

    I did it for you ;-) :

    ;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