Solved

Assembly Language Problem

Posted on 2000-04-16
3
339 Views
Last Modified: 2010-05-18
I have really been battling on this program the whole weekend and I hope that you can help me . What the program does is accept command line parameters -typed in after the program name in dos-
and then it is supposed to reverse those parameters e.g. if the program is called rev.exe then in dos you would type : rev the old man
the  program should print the parameter "the old man" backwards thus the program should print man old the. I have already done about 80% of the program i.e. it grabs the command line parameters and then it scans through those parametrs to find the occurence of an "&" character and everytime it finds that specific character it pushes the offset of that character onto the stack i.e. I uses scasb and the character is stored at ed:di and everytime a match is found di is pushed onto the stack. Now what I wanted to do was to pop a di of the stack and then print every string that way i.e. pop di
mov dx,di and then call dos function 09h to print the string then I want to place a $ at the position [es:di] as when th next di gets popped off dos will only print form that di to the position where I put an $ at [es:di] but for some reason it does not work :

Here is my program I would really appreciate it if you could help me finish this program its really difficult and rather urgent thanks Koopa.
DOSSEG
.MODEL SMALL
.STACK 200h
.DATA
  string db ?

.CODE

PROC Print
  mov ah,09h
  int 21h
ENDP Print


PROC GetCommandLine
;es:di = pointer to command line parameter
;cx = length of string in bytes
;bx = last address of the string

   mov ah,62h
   int 21h
   mov es,bx
   mov bx,81h
   add bl,byte ptr [es:80h]

   mov ch,0
   mov cl,byte ptr [es:80h]

   mov ax,"$"
   mov [es:bx],ax
   mov di,81h
 
   ret
ENDP GetCommandLine


START:
 
  call GetCommandLine   ;initalize es:di basically
  mov al,"&"            ;we are going to search the string
                        ;for a &

  mov dx,0 ;counter=0
  cld      ;count forward basically

  scan:  repne scasb       ;repeat if not equal
    je found               ;if you find somwthing then jump
                           ;to the found section
   found:
   
      push di  ;save the value of di on the stack  
               ;di is the current location of the character
               ;we were looking for
       inc dx   ;counter = counter + 1
       cmp cx,0 ;have we scanned through the whole string    
                ;remember cx was initalized by getCommandLine
      jne scan ;if not carry on scanning remember di is
               ;still the same value as it was
 
    ;After this piece of code everything is put nicely onto the
    ;stack for us now we have to do is pop it off to get the
    ;relative offset values which is stored in di
       
      mov ax,es
      mov ds,ax  ;lets set up our data segment
     
     
       pop di   ;get rid of the last character

       pop di  ;dx = the value on the stack
       mov dx,di

       call print    
 
       mov ax,"$"
       mov [es:di-1],ax

       pop di

               
       mov dx,di
       call print  
   

                 
     
     mov ax,4c00h  ;return control to dos
     int 21h




 END START
0
Comment
Question by:koopa
  • 2
3 Comments
 

Author Comment

by:koopa
ID: 2720945
Please could you also try to put some loop at the end of the question to automatically print the string backwards ?
0
 
LVL 1

Accepted Solution

by:
Rheingold earned 200 total points
ID: 2722091
Hi

here's my solution for this problem. I needed to change the GetCommandLine procedure, because I had problems with the [es:bx] addressing (I'm no DOS-ASM coder; In Win32Asm you have nice 32bit linear addresses ;) - If you get the idea you can easily adjust this to your procedure.

DOSSEG
..MODEL SMALL
..STACK 200h

..DATA
counter db ?
string db ?

..CODE

GetCommandLine PROC
  mov counter, 0 ;; COUNTER FOR PARAMETERS
  pop bx ;; POP THE RETURN ADDRESS FOR THE CALL
  mov ah, 2 ;; FOR DOS-FUNCTION 02h - DISPLAY CHAR
  mov si, 80h ;; COMMANDLINE
  lodsb ;; GET LENGTH OF CMDLINE
  cmp al, 0 ;; LENGHT = 0 ?
  je no_cmdline ;; NO COMMANDLINE
  mov cl, al ;; INITIALIZE COUNTER FOR LOOP
  xor ch, ch
loop1: lodsb
  cmp al, " " ;; NEXT PARAM?
  jne @NoPush
  push si ;; PUSH ADDY OF PARAM ON STACK
  inc counter ;; ONE MORE PARAMETER
  @NoPush:
  loop loop1 ;; GET NEXT CHAR
no_cmdline:
  push bx ;; SAVE RETURN ADDY FOR THE CALL
  ret
GetCommandLine ENDP

START:

call GetCommandLine

@NextParam:
mov dl, 20h ;; PRINT ONE SPACE
int 21h
dec counter ;; DECREASE COUNTER
cmp counter, 0FFh ;; ALL PARAMS PRINTED?
je @NoCmd ;; FINISH
pop si ;; GET ADDY OF PARAMETER
@GoOn:
lodsb ;; GET A CHAR
cmp al, 0Dh ;; PARAMLINE SEEMS TO BE 0D/00 TERMINATED
je @NextParam ;; NEXT PARAM
cmp al, " "
je @NextParam ;; NEXT PARAM

@NotYet:
mov dl, al ;; PRINT
int 21h    ;; CHAR
jmp @GoOn

@NoCmd:
mov ax, 4C00h
int 21h
END START
0
 

Author Comment

by:koopa
ID: 2723747
Thank you so much for the help I really appreciate it !
Koopa
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
draw a Christmas tree by using a nested loop? 26 92
ejb wildfly example 2 69
programming a polycom voip phone 3 52
IDE for Python 5 62
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question