News:

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

Need a push in the right direction.

Started by tibbar_eht, May 11, 2007, 02:32:11 AM

Previous topic - Next topic

tibbar_eht

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

skywalker

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

tibbar_eht

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


skywalker

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

tibbar_eht

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


tibbar_eht

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

skywalker

Here is a list of assembly resources etc.

[attachment deleted by admin]

MichaelW

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

tibbar_eht

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. 

tibbar_eht

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

tibbar_eht

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

MichaelW

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

tibbar_eht

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

MichaelW

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

eschew obfuscation