Help I need to modify the backward procedure in my program

Started by olivarestony, March 27, 2006, 10:16:15 PM

Previous topic - Next topic

olivarestony

Hello Every body I have a program that accept input from the keyboard and it put them backwards example: (hello happy Day!)  backwards: (!yaD yppah olleh), but I want to be : (yaD yppah olleh!) the ! remains at the end of the sentence.
I need to modify the backward procedure to stop at the end of a word not the end of the input of the string. And also can you tell me Where I can move the null check to the main procedure to keep calling backward (for each word)until you get to the null.




INCLUDE Irvine16.inc



   .data
   
   
   source   byte 100 Dup(0)
   target   BYTE 100 Dup(0)

   .code

main: 
   
   MOV AX,@DATA
   MOV DS,AX

   
   mov   dx, OFFSET source      ;display the source string
   
   call   readString
   
   call   crlf
   mov   si, OFFSET source      ;set up the calling parameters(registers)
   mov   di, OFFSET target      
   call   backword         ;reverse the string
   mov   dx, OFFSET target      ;outputthe backward string
   call   WriteString
   call   crlf
   
   .EXIT

backword PROC

   push   ax            ;save any register we modify
   push   si
   mov   al, [si]         ;get the char
   
   cmp   al, [di]            ;check for NULL
   je   nullchar         ;we are done
   inc   si            ;else point to next char
   
   call   backword         ;recursive call
   mov   [di], al         ;save in destination
   inc   di            ;push to the next


nullchar:

   mov   BYTE PTR[di],0         ;save a NULL in case it is the last char
   pop   si            ;restore registers
   pop   ax            
   ret               ;DI has return value


backword ENDp      
      


   
   

END main :(

MichaelW

Hello olivarestony,

The two problems that I can see:

Source is defined as an uninitialized dword, when it needs to be a byte string, like target.

ReadString expects the maximum number of characters that may be typed in CX. The purpose of this is to prevent the user from overfilling the buffer, potentially causing the program to crash.

Did you see the warning that MASM returns?

warning A4003: start address on END directive ignored with .STARTUP

With the recursive calls the backword procedure is more complex than necessary, and difficult to follow, but it works OK :U

eschew obfuscation

olivarestony

Hello Michael. Do you know how to check for space in the string not for null character. Can you help me to modify the program To  get the imput from the user For example: 12345! What I want is 54321! when is reversed

MichaelW

Did you write the code you posted, or did someone else? All you need to do is correct the two problems that I listed. It would also be good to eliminate the warning that MASM returns, either by removing the start address from the END directive, or by replacing the .STARTUP directive with your own startup code.

eschew obfuscation

olivarestony

Hi Michael, I create the code but I need to modify the backward procedure to stop at the end of a word not the end of the input of the string. And also can you tell me Where I can move the null check to the main procedure to keep calling backward (for each word0until you get to the null.

MichaelW

If I correctly understand what you are trying to do, I think I would modify the backword procedure to stop at the first space OR null, and not save a null in the string. Then I would modify main to call the backword procedure in a loop, check the character at DI after each call, substitute a null before calling WriteString and looping, if the character were a space, or exit from the loop if the character were a null.

eschew obfuscation

skywalker

Quote from: olivarestony on March 27, 2006, 10:16:15 PM
Hello Every body I have a program that accept input from the keyboard and it put them backwards example: (hello happy Day!)  backwards: (!yaD yppah olleh), but I want to be : (yaD yppah olleh!) the ! remains at the end of the sentence.


This will do what you're looking for.

; reverse.asm    reverse any string entered(up to 24 chars)
;                      A.K. 3/2006

.model       small
.stack       200h

.data

maximum_string_length     equ          24
string_to_reverse         db           maximum_string_length     dup (?)
reverse_string            db           maximum_string_length     dup (?) 

.code

start:
             mov          ax,@data
             mov          ds,ax
                                      ; clear screen using direct video writes
             mov         ax,0b800h    ; point to color video buffer
             mov         es,ax
             mov         cx,2000
             mov         ah,00000111b
             mov         al,32        ; space character
             mov         di,0         ; di points to start of buffer
             rep         stosw        ; send ax to es:di 4000 times


             mov          ah,3fh
             mov          bx,0
             mov          cx,maximum_string_length   
             mov          dx,OFFSET string_to_reverse
             int          21h
             and          ax,ax
             jz           done
             mov          cx,ax
             push         cx
             mov          bx,OFFSET string_to_reverse
             mov          si,OFFSET reverse_string
             add          si,cx
             dec          si

reverseloop:
             mov          al,[bx]
             mov          [si],al
             inc          bx
             dec          si
             loop         reverseloop
             pop          cx
             mov          ah,40h
             mov          bx,1
             mov          dx,OFFSET reverse_string
             int          21h         
done:             
             mov          ax,4c00h
             int          21h
end     start





ChrisLeslie

This code snippet should do the job as well:

.data
    msg1 db 'hello happy Day!',0
    msg2 db 80 dup(0)             
.code
    start:
      mov esi,0    ; get number of message bytes in esi
      @@:
      mov al,[msg1+esi]
      inc esi
      cmp al,0
      jne @B

      dec esi
      dec esi
      dec esi
      mov edi,0
      @@:         ; this loop does the reversal
      mov al,[msg1+esi]
      mov [msg2+edi],al
      dec esi
      inc edi
      cmp esi,-1
      jne @B
      mov [msg2+edi],'!'

      mov ah,09h     ; display it
      mov dx,offset msg2
      int 21h

      mov  ax,4c00h   ; finish
      int    21h   
end start


Chris