LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2008, 05:10 PM   #1
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
assembly newbie question


While self-learning assembly language (standard 80x86 syntax, using TASM as compiler) I can't seem to get what I'm doing wrong in an exercise taken from a book.

The exercise is like this: reverse the letters of the string like in the following examples:

qwer -> wqre
qwert -> wqret

Aka invert each 2 letters.

Here is my code:

Code:
   DOSSEG
   .MODEL SMALL
   .STACK 100h
   .DATA
MAXIMUM_STRING_LENGTH  EQU  1000
StringToReverse  DB  MAXIMUM_STRING_LENGTH DUP(?)
ReverseString    DB  MAXIMUM_STRING_LENGTH DUP(?)
i dw ?

   .CODE
   mov  ax,@data
   mov  ds,ax                       

; read string:
   mov  ah,3fh                      
   mov  bx,0                        
   mov  cx,MAXIMUM_STRING_LENGTH    
   mov  dx,OFFSET StringToReverse   
   int  21h   

;if nothing is read, end program                   
   and  ax,ax                       
   jz   Done                        
   mov  cx,ax                       
   push cx                          

   mov  bx,OFFSET StringToReverse
   mov  si,OFFSET ReverseString
 
;if only 1 character is read:
   cmp cx,1
   jne NoSingleChar
        mov al,[bx]
        mov [si],al
    jmp Print
    
    
   
NoSingleChar:
   
   mov i,1
   inc si
   
MainLoop:
    mov al,[bx]
    mov [si],al
    inc bx
    dec si
    mov al,[bx]
    mov [si],al
    inc bx
    add si, 3
    add i,1
    cmp cx,i
    je Print
    inc i
    cmp cx,i
    jne MainLoop
    dec si
    mov al,[bx]
    mov [si],al
  
Print:   
   
   pop  cx                          
   mov  ah,40h                      
   mov  bx,1                        
   mov  dx,OFFSET ReverseString     
   int  21h                         
Done:
   mov  ah,4ch                      
   int  21h                         
   END
It works for an odd number of letters, but not for an even number.

E.g. for qwert it shows tqre and I can't figure out why it does that.

What I also don't get... the AX register, which is supposed to contain the number of characters read after the 3fh dos function, according to the debugger, contains the number of characters +2... Aka for qwer it will hold 6, why is that?

Anyway, the main question: what am I doing wrong? Any suggestion or idea is welcomed.
 
Old 11-06-2008, 10:08 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
While I find it odd posting Dos questions on the Linuxquestions forum I'll try to be helpful. A quick look at your code seems rather confused - this is after all a simple problem involving an increasing source and decreasing destination index - one initialised to the string beginning the other to the target start + string length-1. Try to map this process out into pseudo code and it should be easy to translate this to assembler.

I'm not familar with TASM but a powerfull tool is always a single stepping debugger - like Dos's DEGUG (or the more powerful SYMDEB - CODEVIEW came later from Micrsoft). TASM may have an equivalent allowing symbolic debugging. You can then step through your code examining registers and memory locations to easily spot mistakes.

As to the interrupt returning unexpected values - perhaps the string length includes a CR + LF on the end of the string - just a guess but debugging should make this obvious.

Good Luck with this.
 
Old 11-06-2008, 10:50 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bgeddy View Post
While I find it odd posting Dos questions on the Linuxquestions forum I'll try to be helpful. A quick look at your code seems rather confused - this is after all a simple problem involving an increasing source and decreasing destination index - one initialised to the string beginning the other to the target start + string length-1. Try to map this process out into pseudo code and it should be easy to translate this to assembler.

I'm not familar with TASM but a powerfull tool is always a single stepping debugger - like Dos's DEGUG (or the more powerful SYMDEB - CODEVIEW came later from Micrsoft). TASM may have an equivalent allowing symbolic debugging. You can then step through your code examining registers and memory locations to easily spot mistakes.

As to the interrupt returning unexpected values - perhaps the string length includes a CR + LF on the end of the string - just a guess but debugging should make this obvious.

Good Luck with this.
This is what written at the page top (emphasis is mine):


Quote:
This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
 
Old 11-06-2008, 11:40 AM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
This is what written at the page top (emphasis is mine):
Heh - do you know I've never noticed that ! Anyway - I'm not wishing to be judgmental here just trying to help.. Hopefully this is how I came across.

To the OP - please ignore my ill informed comments about Dos questions in a Linux forum.
 
Old 11-06-2008, 12:48 PM   #5
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Original Poster
Rep: Reputation: 30
Thanks for your replies.

First of all,I got it fixed... it was related to the number of characters, the weird +2 value that cx had.

Yes, I am aware it is a Linux forum, and that it's the non-*nix section :P

You guys always helped me out, wether with programming or linux questions, so it didn't seem strange to me.

Plus, I'm learning the DOS-version of assembly only because you can find more resources about it, my final goal shall be, of course, some linux kernel device drivers and other fun stuff :P

How was that quote... "don't you miss the days when a man was a man and wrote his own device drivers?" or something like that
 
Old 11-06-2008, 01:05 PM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by Valkyrie_of_valhalla View Post
How was that quote... "don't you miss the days when a man was a man and wrote his own device drivers?" or something like that
yeah, back when devices were connected via serial ports and used standard interfaces
 
Old 11-07-2008, 09:24 AM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by H_TeXMeX_H View Post
yeah, back when devices were connected via serial ports and used standard interfaces
This is what we still do in Linux... since 1970-ies we have /dev/ttyS0 and that has never changed since.

It was a (bad) habit introduced in DOS(*) that hardware was addressed immediately from any program, and yes, in some way you could call
Code:
mov ax,1F
out 378,ax
a standard way to talk to a serial device.

(*) Well maybe it was not really introduced in DOS, we had the AppleII and the Commodore etc before that, but DOS was the first platform were [commercial] programs became available in such vast quantities and still using those awkward techniques. Although the intention of the DOS architecture was to do it well-behaved thru a BIOS function.

Last edited by jlinkels; 11-07-2008 at 01:51 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembly Question aceman817 Programming 1 02-28-2006 01:01 AM
Inline Assembly Question tjt Programming 3 08-08-2004 04:38 AM
C & Assembly question eantoranz Programming 3 04-23-2004 01:18 PM
Assembly Question! wwnn1 Programming 4 06-16-2002 01:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration