The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: tibbar_eht on May 11, 2007, 02:32:11 AM

Title: Need a push in the right direction.
Post by: tibbar_eht on May 11, 2007, 02:32:11 AM
Hello out there.  I am currently taking a class in Assembly Language and I have run into some difficulty in an assignment I have.  The object is to have a 12 character string display forward then backwards then forwards again.  I can get the string to display one way or the other but not both.  I am a bit lost as to what I have wrong.  My code is as follows:


TITLE Reverse

.model small
.stack 64

.data

tibbar1 db 'USS KITTY HAWK', '$'
tibbar2 db 14 dup ('*'), '$'

.code

main PROC far


mov ax, @data
mov ds, ax
mov es, ax
mov cx, 18h
lea si, tibbar1
lea di, tibbar2

A10:
mov al, [si]
mov [di], al
inc si
inc di
dec cx
jnz A10 


A20:

mov al, [si]
mov [di+0dh], al
inc si
dec di
dec cx
jnz A20

mov ah, 06h
mov al, 00h
mov bh, 00bch
mov cx, 0000h
mov dx, 184fh
int 10h

mov ah, 09h
lea dx, tibbar2
int 21h
mov ax, 4c00h
int 21h


MAIN ENDP
END MAIN



I have asked my instructor for help but he teaches as a side gig to his main job so he is not very timely in getting back.

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: skywalker on May 11, 2007, 02:58:17 AM
Maybe this will help some.

; reverse.asm    reverse any string entered
;

.model       small
.stack       200h

.data
maximum_string_length     equ          10
string_to_reverse         db           maximum_string_length     dup (?)
reverse_string            db           maximum_string_length     dup (?) 

.code

start:
             mov          ax,@data
             mov          ds,ax
             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
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 11, 2007, 03:28:06 AM
So where would I put the string that needs to be reversed?  Is it in this line:
maximum_string_length     equ          10

Thank you for your help in this.

Cheers,
Jay

Title: Re: Need a push in the right direction.
Post by: skywalker on May 11, 2007, 03:56:55 AM
The example I posted waits for the user to input a string. After he hits enter, then it reverses it and shows it on the s screen.

It's been a while since I did any 16 bit coding. Look for Ralf Brown's Interrupt List which tells all the functions
of DOS assembly.

Andy
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 11, 2007, 03:58:57 AM
Ahh ok that is why it was not doing anything when I ran it.  We are still early in the term and have not quite got to the point of handeling input from a user yet. 

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: skywalker on May 11, 2007, 04:04:00 AM
You can find the interrupt list here.

http://www.cs.cmu.edu/~ralf/files.html
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 11, 2007, 04:41:48 PM
Thanks for the list.  It is a bit over my head but Iam sure once Iget a little more comfortable in coding I will be able to use it better.  Although I still am unsure what I need to do to get the display I need.  Sometimes I really wish I could get a good teacher for once in a programing class.

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: skywalker on May 11, 2007, 05:41:50 PM
Here is a list of assembly resources etc.

[attachment deleted by admin]
Title: Re: Need a push in the right direction.
Post by: MichaelW on May 11, 2007, 06:10:56 PM
QuoteThe object is to have a 12 character string display forward then backwards then forwards again.
I see nothing in this description that requires you to reverse the string. If you displayed the string a character at a time, using interrupt 21h function 2 for example, you could loop through the characters forwards or backwards to generate the display. You could use any base or index register to access the individual characters, for example:

mov dl, [si]

To progress through the string forwards you could load the base or index register with the offset address of the string (I would use the OFFSET operator) and the counter with the length of the string (I would use the LENGTHOF operator). After each character you could increment the base or index register and decrement the counter, continuing until the counter reached zero. To progress through the string backwards you could load the base or index register with the offset address of the string, plus the length of the string, minus one (I would use an expression of the form "OFFSET thestring + LENGTHOF thestring - 1"). After each character you could decrement the base or index register and decrement the counter, continuing until the counter reached zero.

To move the cursor to the next line you could display a carriage return character (ASCII 13) followed by a line feed character (ASCII 10).

BTW, with this method you would have no need for the trailing '$'.
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 12, 2007, 12:06:08 AM
I feel quite the villiage idiot.  I have a general understanding of what you are saying but don't quite know how to type the code to do that.  Man I wish I had some one close by that knew what they were doing to teach me. 
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 12, 2007, 06:36:08 PM
Ok after a day of banging my head against the wall I still have not figured out what to do.  I guess I need someone to hold my hand and walk me through this. 

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 13, 2007, 06:46:50 PM
my instructor finially wrote back but I am not sure what he means exactly.  I am so frustrated right now.  Any this is what he said:

Quotehi!! ... i went over your attachment ... the first loop is moving string from one place to the other as is, but then for loop 2 you use lea to put address in bx ?? , not in e or d ?? ... this is why it doesn't work ... only si and di handle data addresses ... try now ... aloha!!

Can anyone help me figure out what he means?

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: MichaelW on May 13, 2007, 08:32:04 PM
I don't know what your code currently looks like, so this is just a guess. For 16-bit code, where most of the instructions can use any base (BX, BP) or index (SI, DI) register for indirect memory operands, LODS_ uses only SI, STOS_ and SCAS_ only DI, and MOVS_ only SI and DI.
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 13, 2007, 08:53:19 PM
The code that I sent his is

TITLE Reverse

.model small
.stack 64

.data

tibbar1 db 'USS KITTY HAWK', '$'
tibbar2 db 14 dup ('*'), '$'
;tibbar3 db 14 dup ('*'), '$'
.code

main PROC far
;.startup

mov ax, @data
mov ds, ax
mov es, ax
mov cx, 18h
lea si, tibbar1
lea di, tibbar2

A10:
mov al, [si]
mov [di], al
inc si
inc di
dec cx
jnz A10  ;JUMP AND DO THE LOOP AGAIN!!

;mov ah, 06h
;mov al, 00h
;mov bh, 00bch
;mov cx, 0100h
;mov dx, 184fh
;int 10h

;mov ah, 09h
;lea dx, tibbar2
;lea bx, tibbar3
;int 21h
;mov ax, 4c00h
;int 21h


A20:

mov al, [si]
mov [di+0dh], al
inc si
dec di
dec cx
jnz A20  ;JUMP AND DO THE LOOP AGAIN!!

mov ah, 06h
mov al, 00h
mov bh, 00bch
mov cx, 0000h
mov dx, 184fh
int 10h

mov ah, 09h
lea dx, tibbar2
;lea bx, tibbar3
int 21h
mov ax, 4c00h
int 21h

;.exit
MAIN ENDP
END MAIN



I know there is a lot of extra lines but I was trying to mix and match with out having to retype things.  I just can't get the basics I guess.    :'(

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: MichaelW on May 13, 2007, 10:38:42 PM
I could not figure out what you are trying to do with parts of your code, so I modified the first loop to reverse the string as it was copied, and eliminated the buggy second loop.

TITLE Reverse

.model small
.stack 64

.data

tibbar1 db 'USS KITTY HAWK', '$'
tibbar2 db 14 dup ('*'), '$'
;tibbar3 db 14 dup ('*'), '$'
.code

main PROC far
;.startup

mov ax, @data
mov ds, ax
mov es, ax

  ; REVERSE THE STRING WHILE COPYING IT

  ;mov cx, 18h          ; ?????

  MOV CX, 14

lea si, tibbar1

  ;lea di, tibbar2

  ; THE "- 2" IS NECESSARY TO AVOID THE '$'
  MOV DI, OFFSET tibbar2 + SIZEOF tibbar2 - 2

A10:
mov al, [si]
mov [di], al
inc si

  ;inc di

  DEC DI

dec cx
jnz A10  ;JUMP AND DO THE LOOP AGAIN!!

;mov ah, 06h
;mov al, 00h
;mov bh, 00bch
;mov cx, 0100h
;mov dx, 184fh
;int 10h

;mov ah, 09h
;lea dx, tibbar2
;lea bx, tibbar3
;int 21h
;mov ax, 4c00h
;int 21h

COMMENT | THEN YOU HAVE NO NEED FOR THIS LOOP
A20:

mov al, [si]
mov [di+0dh], al
inc si
dec di
dec cx
jnz A20  ;JUMP AND DO THE LOOP AGAIN!!
|

mov ah, 06h
mov al, 00h
mov bh, 00bch
mov cx, 0000h
mov dx, 184fh
int 10h

mov ah, 09h
lea dx, tibbar2
;lea bx, tibbar3
int 21h
mov ax, 4c00h
int 21h

;.exit
MAIN ENDP
END MAIN

Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 13, 2007, 10:58:35 PM
Ok my problem is getting a display of a forward string and a backwards string. It should display
USS KITTY HAWK
    KWAH YTTIK USS


I cleaned up the code so the extra lines are gone. 


TITLE Reverse

.model small
.stack 64

.data

tibbar1 db 'USS KITTY HAWK', '$'
tibbar2 db 14 dup ('*'), '$'
.code

main PROC far

mov ax, @data
mov ds, ax
mov es, ax

  ; REVERSE THE STRING WHILE COPYING IT


  MOV CX, 14

lea si, tibbar1

  ;lea di, tibbar2

  ; THE "- 2" IS NECESSARY TO AVOID THE '$'
  MOV DI, OFFSET tibbar2 + SIZEOF tibbar2 - 2

A10:
mov al, [si]
mov [di], al
inc si

  ;inc di

  DEC DI

dec cx
jnz A10  ;JUMP AND DO THE LOOP AGAIN!!



COMMENT | THEN YOU HAVE NO NEED FOR THIS LOOP
A20:

mov al, [si]
mov [di+0dh], al
inc si
dec di
dec cx
jnz A20  ;JUMP AND DO THE LOOP AGAIN!!
|

mov ah, 06h
mov al, 00h
mov bh, 00bch
mov cx, 0000h
mov dx, 184fh
int 10h

mov ah, 09h
lea dx, tibbar2
int 21h
mov ax, 4c00h
int 21h


MAIN ENDP
END MAIN


I tried to have it display the string forward in the second loop by changing:
mov [di+0dh], al
inc si
dec di

to
mov [di], al
inc si
inc di


But that didn't work.

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: MichaelW on May 13, 2007, 11:50:28 PM
The modified code I posted correctly reverses the string, and displays the reversed string. The purpose of terminating strings with a '$' is normally so they can be displayed with Interrupt 21h, function 9. The code that displays the reversed string is:

mov ah, 09h
lea dx, tibbar2
int 21h

So code to display the original string would be:

mov ah, 09h
lea dx, tibbar1
int 21h


Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 13, 2007, 11:56:43 PM
Ok I figured out why the second loop was not working.  I was not familiar with the pipe's function .  Now when it displays I get KWAH YY HAWK.  So it looks like it is reversing and printing forward but printing it over each other. 

Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: Tedd on May 14, 2007, 10:37:41 AM
Run through it by hand on paper and you'll see why it's happening - and that you need to swap two characters at the same time (if you have to do it in-place.)
Title: Re: Need a push in the right direction.
Post by: tibbar_eht on May 20, 2007, 05:06:33 AM
Just thought I would post the code that finially worked.


TITLE try1

.model small
.stack 64

.data

tibbar1 db 'USS KITTY HAWK', '$', 0
tibbar2 db 14 dup ('*'), '$', 0
tibbar3 db 14 dup ('*'), '$', 0
.code

main PROC far

mov ax, @data
mov ds, ax
mov es, ax

  ; REVERSE THE STRING WHILE COPYING IT


mov cx, 0eh

lea si, tibbar1
lea di, tibbar2


A10:
mov al, [si]
mov [di+0dh], al
inc si
    dec di
dec cx
jnz A10  ;JUMP AND DO THE LOOP AGAIN!!

mov ah, 06h
mov al, 00h
mov bh, 00bch
mov cx, 0000h
mov dx, 184fh
int 10h
mov ah, 02h
mov dh, 11
mov dl, 34
int 10h

mov ah, 09h
lea dx, tibbar2
int 21h
mov cx, 0eh
lea si, tibbar2
lea di, tibbar3

A20:

mov al, [si+0dh]
mov [di], al
dec si
inc di
dec cx
jnz A20  ;JUMP AND DO THE LOOP AGAIN!!



mov bh,00h
mov ah, 02h
mov dh, 14
mov dl, 34
int 10h
mov ah, 09h
lea dx, tibbar3
int 21h
mov ax, 4c00h
int 21h


MAIN ENDP
END MAIN


Cheers,
Jay
Title: Re: Need a push in the right direction.
Post by: eek on May 20, 2007, 11:36:06 AM
You can run up and down on the string using a video offset and the direction flag.
(I put an int16 in there to stop it going bonkers)

ZONE                       S L
org cs:100
;print.reverse.print forever
PRP
nop  ;ZRS
mov bp,B800 ;video
mov,es,bp
mov di,722  ;mid screen
mov si,USS  ;start of string
Start
mov cx,E
Print
movsb
inc di
loopnz Print
dec si
inc di
inc di
std
mov cx,E
tnirP
movsb
inc di
inc di
inc di
loopnz tnirP
cld
inc si
inc di
inc di
int 16
jmp Start
ret
USS ch10 USS KITTY HAWK
END
______________


Dont unzip it.
Rename the file extension from "zip" to "com", click it to start in DOS and hit any key to print out the output.



[attachment deleted by admin]