The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: new_comer on April 06, 2006, 04:56:56 PM

Title: An Interesting problem
Post by: new_comer on April 06, 2006, 04:56:56 PM
Greetings everyone,

I have been assigned to do following:

If Input is :   THIS IS A TEST

(Optional)Intermediate output: TSET A SI SIHT ( In reversed order) I have achieved this using stacks.

Final Output should be: SIHT SI A TSET (Preserving the order of words) But dont know how to achieve this



If anyone know the algo or code to do this

TIA

Title: Re: An Interesting problem
Post by: Tedd on April 06, 2006, 06:14:10 PM
Consider what you actually did for the first part (reverse a string,) and then consider what you need to do for the second part (reverse a word.)
A word is still just a string, and you already know how to reverse a string :wink
The only difference is that this time the strings end at a space. And there will probably be more than one.
It's a simple step from what you've already done.

I could spell it out more than this, but what would be the fun in that? :bdg
Have a go, you might surprise yourself :U
Title: Re: An Interesting problem
Post by: new_comer on April 06, 2006, 07:37:50 PM
Thanks for the reply, incidently what you just instructed to do , I already knew that. I just needed to know how to implement that into Assembly code

Well here is my code (Please tell me what should I add to get final output)
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
;display user prompt

    MOV AH,2
    MOV DL,'?'
    INT 21H

    XOR CX,CX

    MOV AH,1
    INT 21H

;push input into the stack till ENTER is not pressed
WHILE_:
    CMP AL,0DH ;CR?
    JE END_WHILE

    PUSH AX 
    INC CX

    INT 21H
    JMP WHILE_

END_WHILE:

    MOV AH,2
    MOV DL,0DH
    INT 21H

    MOV DL,0AH
    INT 21H
    JCXZ EXIT; exit if no  characters read

TOP:
    POP DX

    INT 21H ;display it
    LOOP TOP

EXIT:
    MOV AH,4CH
    INT 21H
MAIN ENDP
     END MAIN

Title: Re: An Interesting problem
Post by: Tedd on April 07, 2006, 10:54:43 AM
If you already know it, then what is so difficult?
You push the input onto the stack until it's a space (instead of a CR) -- that's a massive change of two characters to your current program.
Then you're almost finished. You'll just need to add another test for when CR is input - so you print the word and then end, instead of continuing.
Title: Re: An Interesting problem
Post by: new_comer on April 07, 2006, 02:07:39 PM
Thanks again for reply,

I used CR to take input till user doesnt press return
For example:

?this is a test (return pressed)
tset a si sith  <-Output


But my objective is to get

sith si a tset (Prserving the order of word but reversing the order of alphabets)

I guess I am making myself clear to you.


I wud really appreciate, if you wud kindly write some code.




Title: Re: An Interesting problem
Post by: new_comer on April 07, 2006, 02:18:16 PM
Quote from: Tedd on April 07, 2006, 10:54:43 AM
If you already know it, then what is so difficult?
You push the input onto the stack until it's a space (instead of a CR) -- that's a massive change of two characters to your current program.
Then you're almost finished. You'll just need to add another test for when CR is input - so you print the word and then end, instead of continuing.


But sir, there is only one stack, how I gonna do that... kindly bear with me, after all I am "new comer" :)
Title: Re: An Interesting problem
Post by: rags on April 07, 2006, 03:36:22 PM
Quote from: new_comer on April 07, 2006, 02:07:39 PM

I wud really appreciate, if you wud kindly write some code.

I think Tedd  clearly explained what is needed to be done.
Asking people to write the code for you, usually doesn't get a response
Title: Re: An Interesting problem
Post by: Tedd on April 07, 2006, 05:15:04 PM
No-one here is going to write the code for you, so if that's what you're expecting/wanting you can save yourself time and energy by giving up now :P

On the other hand, if you're still having difficulty in understanding what I'm telling you (yes, I understand what you want to do perfectly!)... let me be even more verbose.

There is only one stack - good observation.
So, the first program takes the user input, puts the letters on the stack, and then prints all of the letters when CR is pressed.
For the second program you want to put the letters on the stack, and print all of the letters (for that word) when SPACE is pressed. This will reverse one word. Then you need to repeat the same thing again, and again, and again -- printing every word, each time the user presses SPACE. You keep repeating until the user presses CR - then you finish (not forgetting to print the last word.)

Now, assuming you understand the method of doing this (as explained above, and in my previous posts!) what part of the implementation do you find difficult?



[I have no problem helping, or explaining things in detail if needed. But you are really starting to push my patience :naughty:]
Title: Re: An Interesting problem
Post by: P1 on April 07, 2006, 05:54:31 PM
Quote from: new_comerThe one which are written on my college text book.
This is Homework.

This is a standard data manipulation assignment.  Design to test a student's ability to structure a solution with a given a set of tools.

If they can learn in this situation, and they can basically, program using any other set of tools.

new_comer,

You are Welcome here, but realize the limitations your expected to meet.  INCLUDING your Instructor's  :naughty:

Forum 'Search' is your friend, along with Google.   

Please read the forum rules.

As a student of this discipline, use this forum to develop your skills and techniques.


Regards,  P1  :8)
Title: Re: An Interesting problem
Post by: new_comer on April 07, 2006, 06:08:37 PM
[Please Forgive my ingnorance] I am a newbie and just joined the college.

I really apprecite you for helping me.

But sir, my domain of knowledge is limited to solving this problem using 'push' and 'pop' .

Let's Suppose,If  I take input from user till space is pressed, the input will be pushed into stack and poped out (hence a single word will  be reversed), it will happen with every word user enters. Like there will be seperate outputs for everyword enterd.... but I need an output of whole sentence at once as user press return.

I really cannot figure our how to implement this.
Title: Re: An Interesting problem
Post by: new_comer on April 07, 2006, 06:44:16 PM
Quote from: P1 on April 07, 2006, 05:54:31 PM
Quote from: P1 on April 07, 2006, 05:38:12 PM
Quote from: new_comerThe one which are written on my college text book.
This is Homework.

This is a standard data manipulation assignment.  Design to test a student's ability to structure a solution with a given a set of tools.

If they can learn in this situation, and they can basically, program using any other set of tools.

new_comer,

You are Welcome here, but realize the limitations your expected to meet.  INCLUDING your Instructor's  :naughty:

Forum 'Search' is your friend, along with Google.   

Please read the forum rules.

As a student of this discipline, use this forum to develop your skills and techniques.


Regards,  P1  :8)




Sir, this not homework but the problem is menifestation of my mind... the home work part was till first part of problem.

As a matter of fact, I learned alot through this forum and my first assebly code was culimination of this forum itself.
Title: Re: An Interesting problem
Post by: MichaelW on April 07, 2006, 07:21:31 PM
If you need to display the sentence all a once, after each complete word has been pushed, pop the characters into a buffer instead of displaying them. Then when the user presses return, append a "$" to the end of the buffer and display the contents.

Title: Re: An Interesting problem
Post by: new_comer on April 07, 2006, 09:02:38 PM
Voila... you got it... I needed just that...  I am asking that instructions name to do that.


see if I define

.DATA
OUTPUT DB ?,'$'


How I gonna put subsequent char/strings into output string.Or is there anyother method to do such things.

I am just asking the instruction name.. not whole code.. I guess that someone wont mind writing it down.
Title: Re: An Interesting problem
Post by: MichaelW on April 07, 2006, 10:07:18 PM
The buffer needs to be large enough to hold the entire string, for example:

buffer db 100 dup('$')

Filling the buffer with '$' allows you to avoid having to append a '$' to the end of the buffer in a separate operation, but note that this will generally work only for the first string stored in the buffer.

In this case you need to dynamically index the buffer, and to do this from 16-bit code you can use an indirect memory operand with the memory address stored in a base or index register. For example:

; load bx with the offset address of the buffer
...
mov  BYTE PTR[bx], X  ; copy the contents of X (a byte value or register) to the buffer
; adjust bx to the next byte address
...
Title: Re: An Interesting problem
Post by: new_comer on April 08, 2006, 04:16:55 PM
 :dance: I have got enough cue and help, now I can refine my search and concentrate on specific things to study.. sooner I will be posting the complete code.. so that some other newbie can get help.
Title: Re: An Interesting problem
Post by: new_comer on April 09, 2006, 10:19:54 PM
Finally I wrote that program (Actually my high level programming skill helped me alot)

Well.. thanks to tedd sir and MichaelW sir and other who helped me through pm.

here is the code:



TITLE PGM_new_comer: Reversing the order of characters

.MODEL SMALL
.STACK 100H
.DATA
STR1 DB 200 DUP('$')
OUTPUT DB "OUTPUT: $"
.CODE
MAIN PROC
;display user prompt
    MOV AX,@DATA
    MOV DS,AX

    MOV AH,2
    MOV DL,'?'
    INT 21H

    LEA DI,STR1
    XOR CX,CX

    MOV AH,1
    INT 21H

WHILE_:
;Store User Input to STR1 string

    CMP AL,0DH ;CR?
    JE END_WHILE

    MOV [DI],AL
    INC DI

    INC CX

    INT 21H
    JMP WHILE_



END_WHILE:

    MOV AL,' '
    MOV [DI],AL  ;Adding a blank space at the end of the string,makes logic implementation easier

    MOV AH,2
    MOV DL,0DH
    INT 21H

    MOV AH,2
    MOV DL,0AH
    INT 21H

    MOV AH,9
    LEA DX,OUTPUT
    INT 21H

    XOR CX,CX
    LEA SI,STR1
    CLD

LOOP_1:
      LODSB

      CMP AL,'$' ;Check for end of the string
      JE EXIT

          PUSH AX
          INC CX

          CMP AL,' '
          JNE LOOP_1

          LOOP_22:

               MOV AH,2
               POP DX
               INT 21H                       
               LOOP LOOP_22

          JMP LOOP_1

EXIT:
    MOV AH,4CH
    INT 21H

MAIN ENDP
    END MAIN
Title: Re: An Interesting problem
Post by: new_comer on April 10, 2006, 02:41:43 PM
Though my program is running fine, I guess a bit of optimization will not hurt :)


I wonder if any body knows the alternative to this part of code

[b]STR1 DB [color=Red]200[/color] DUP('$')[/b]

Actually, I am fixing the string length.. is there any way to dynamically allocate the length... like we have malloc() func in C.
Title: Re: An Interesting problem
Post by: Tedd on April 10, 2006, 05:12:53 PM
There are some INT functions for 'allocating' memory, but I'm not sure of the details.
You can probably just use the stack (sub sp,200) -- but make sure you use an even number (end with 0, 2, 4, 6, 8)!!
And note that the contents of this memory will not be initialised - so you should fill it with "$$" if you choose.
Title: Re: An Interesting problem
Post by: MichaelW on April 10, 2006, 06:20:13 PM
You can use Interrupt 21h function 48h to allocate memory, and function 49h to free the allocated memory, but how much you can allocate depends on how much free memory is available, which typically will not be enough to be useful. On my system, running under Windows 2000, a typical small DOS EXE can allocate only 6 paragraphs = 96 bytes. There is a field in the EXE header that controls the maximum amount of memory that is to be allocated for the program, but to my knowledge LINK does not provide any option to control this value. The normal procedure is to resize the program's memory block using function 4Ah, freeing unnecessary memory above the program.

For a small buffer it would be easiest to allocate it in the initialized data segment (.DATA). Allocating it in the uninitialized data segment (.DATA?) would be the next easiest, followed by allocating it from the stack, and either of these methods would avoid having the buffer stored in the EXE, but you would need to initialize it, or better IMO, alter your code to append a "$" to the end of the data after it is stored.

Title: Re: An Interesting problem
Post by: new_comer on April 10, 2006, 08:42:46 PM
Thanks, that was very informative.