LinuxQuestions.org
Visit Jeremy's Blog.
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 09-23-2012, 07:30 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
X86 Assembly MS DOS int 21h 0x09 wierdness


Hello everyone, I know this is a Linux forum but I was wondering if anyone could tell me why the below code acts oddly with DOS printing function? Program is compiled using NASM with nasm -o test.com test.asm. I am running the program in a command prompt on windows 7 but have also tested it with a win98 boot disk and I get the same result?

Here is the code
Code:
 
org 100
bits 16 

 

section .data 
garbage: db "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" 
message: db "Good message." ,'$'


section .text


mov ah, 0x09 
mov dx, message
int 21h 

xor ax, ax 
int 21h
Here is the output
Code:
F:\OS_Files>test.com

             ┤       ║Ä ═!1└═! zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzGood message.
F:\OS_Files>
 
Old 09-24-2012, 05:54 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Is it a .com file? Or an .exe? Shouldn't you set the segment registers first?
 
Old 09-24-2012, 06:04 AM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Shouldn't you load the address of the message string with lea, instead of mov?
 
1 members found this post helpful.
Old 09-24-2012, 10:31 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Quote:
Is it a .com file? Or an .exe? Shouldn't you set the segment registers first?
This is a .com file and the org 100 sets all the registers to the correct values. I could change them if I wanted to.


Quote:
Shouldn't you load the address of the message string with lea, instead of mov?
It does not matter. Both statements in NASM do the same thing. I think lea creates slower code. You can change the mov dx, message to lea dx, [message] in the code and it does exactly the same thing.

See this post for more clarity on this if you care

Last edited by exvor; 09-24-2012 at 10:38 PM.
 
1 members found this post helpful.
Old 09-25-2012, 02:43 AM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by exvor View Post
It does not matter. Both statements in NASM do the same thing. I think lea creates slower code. You can change the mov dx, message to lea dx, [message] in the code and it does exactly the same thing.

See this post for more clarity on this if you care
Thanks for the pointer. Never knew that
 
Old 09-25-2012, 02:57 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I'd prefer org 256 or org 0x100
 
Old 09-27-2012, 10:37 AM   #7
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Just out of curiosity, what's the "$" for?

Are you sure it shouldn't be mov dx, [message] ? i.e. with big brackets? (been a while since I dabbled in stuff like this, would be good to get my skills up again! )
 
Old 09-27-2012, 12:07 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by exvor View Post
Code:
 mov dx, message
That loads into dx the offset of message from the beginning of the segment (.data) not its offset from the location pointed to by the ds register in a .com file.

It has been too many years since I did such things for me to remember the correct syntax in nasm to refer to offsets within a .com

In masm, you would declare a group (typically named dgroup) containing all the segments and then specify the offset relative to dgroup.

Last edited by johnsfine; 09-27-2012 at 12:09 PM.
 
1 members found this post helpful.
Old 09-27-2012, 01:32 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
'$' is the string-terminator character in some obsolete INT 21H call (CP/M legacy).
 
Old 11-27-2012, 07:45 PM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Alright I figured out what I was doing wrong :P and I am stupid. NevemTeve had it right when I should have changed the org to org 0x100 instead of org 100 as they are different origin addresses and the way NASM works is to use that origin as the offset for everything else in the program.

johnsfine - NASM works a bit different then MASM with the org directive so I think that's what might have been throwing you off there.

NASM will load the offset from the Origin specified from the ORG directive rather then the beginning of the segment.
 
Old 11-28-2012, 09:20 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is the register pair DS : DX that is the pointer to the string. Its been a while, and I never really did use NASM, but I think you need to explicitly load DS with the segment address of the .data segment.

--- rod.
 
Old 11-28-2012, 09:55 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by theNbomr View Post
It is the register pair DS : DX that is the pointer to the string. Its been a while, and I never really did use NASM, but I think you need to explicitly load DS with the segment address of the .data segment.
That is a DOS issue, not a NASM issue. Your suggestion could be used, but doing it that way is neither practical nor common.

In a .com file in DOS (or DOS mode of other OS's) the PSP (program segment prefix), code segment, data segment, stack, etc. are all treated at run time as if they were merged together into a single segment. The OS loads the address of that segment into CS, DS and SS (I think ES as well, but I don't recall) at the start of execution. It is common and practical for a .com program to leave CS, DS, and SS unchanged through the entire execution.

So DX should be the offset of the string from the beginning of the PSP, not from the beginning of the data segment. There are multiple ways to accomplish that in NASM, depending on details of the full build process from source code to .com file. Using a group directive (similar to what you might do in MASM) is the most general method, suitable for situations in which you assemble to .obj files and then use a linker to create the .com or use a linker to create a .exe and another tool to convert to .com.

Using just the correct org directive without a group directive can handle some of the simpler situations and/or build sequences.
 
Old 11-28-2012, 11:00 AM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,633
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
"It's all com - ing back to me now ..."

(click here if you can stand to here Céline Dione actually singing ... I'll pass. )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 language about interrupt 21h,thanks topheraholic Programming 4 06-20-2011 07:44 AM
[SOLVED] Assembly language printing integers to the stdout using int 0x80 yaami Programming 9 07-08-2010 10:43 PM
understanding DOS vs Linux assembly language shortname Programming 10 03-07-2007 10:04 PM
x86 assembly programming in Linux XsuX Programming 9 12-01-2004 09:45 AM
x86 assembly: error message mandrake_linux Programming 1 06-12-2001 09:00 AM

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

All times are GMT -5. The time now is 10:41 AM.

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