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

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

new_comer

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.

Tedd

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

MichaelW

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.

eschew obfuscation

new_comer

Thanks, that was very informative.