News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

An Interesting problem

Started by new_comer, April 06, 2006, 04:56:56 PM

Previous topic - Next topic

new_comer

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


Tedd

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
No snowflake in an avalanche feels responsible.

new_comer

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


Tedd

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.
No snowflake in an avalanche feels responsible.

new_comer

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.





new_comer

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" :)

rags

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
God made Man, but the monkey applied the glue -DEVO

Tedd

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:]
No snowflake in an avalanche feels responsible.

P1

#8
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)

new_comer

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

new_comer

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.

MichaelW

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.

eschew obfuscation

new_comer

#12
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.

MichaelW

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
...
eschew obfuscation

new_comer

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