Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Assembly problems (can't get helloworld working).
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Sat Nov 11, 2006 5:09 pm    Post subject: Assembly problems (can't get helloworld working). Reply with quote

The code I've been given for an assembly helloworld is below.

Code:
CR equ 13
LF equ 10

   JMP START

MESSAGE:
   DB "Hello, world!",CR,LF,"$"

START:
   MOV DX, MESSAGE
   MOV AH,9
   INT 21h

   MOV AH, 4Ch
   INT21h


I cant get this to work using the netwide assembler in xubuntu. I get a binary output file but I cant execute it (I've checked the permissons). It gives

bash: ./helloworld.o: cannot execute binary file


The code was written with Windows in mind but I thought at least helloworld will be portable.
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Mon Nov 13, 2006 10:02 am    Post subject: RE: Assembly problems (can Reply with quote

Nay, that's for DOS. The 'int 21h' line activates the main DOS interrupt, with a parameter of '9' in the AH register to say 'print the string stored in DX'. Assembly isn't really portable between OSes -- you use the same instructions, but the way of communicating with the OS is different. In Linux, for instance, you use 'int 80h' to call OS features.

Here's a great tutorial with a Linuxised 'Hello world' program:

http://docs.cs.up.ac.za/programming/asm/derick_tut/

See the code in blue further down. Good luck!

Mike
Back to top
View user's profile Send private message Visit poster's website
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Tue Nov 14, 2006 3:55 pm    Post subject: RE: Assembly problems (can Reply with quote

Cheers for that, unfortunately I have to do it DOS style. I was mostly checking to see if I could get it to work on both at this level. Perhaps time to pull out FreeDOS in VMware so I don't have to mess around rebooting.
Back to top
View user's profile Send private message
kilikopele
LXF regular


Joined: Wed Aug 09, 2006 4:35 pm
Posts: 298

PostPosted: Wed Nov 15, 2006 3:20 am    Post subject: RE: Assembly problems (can Reply with quote

Just a side question on this: Is HLA (high-level assembler) a real assembler or more like a pseudo-assembler?

Also, if you're not making Menuet or programming for embedded gear, what are the real-world occasions where someone might choose assembler over C / C++?
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Wed Nov 15, 2006 10:32 am    Post subject: RE: Assembly problems (can Reply with quote

You need assembler to do some of the low-level bootstrapping parts of an OS, and it's also often used in some speed-critical routines in various OS kernels. But no, there's really no point writing a web browser or office suite in assembler -- the small speed boosts you'd gain would be massively offset by the difficulties in debugging.

Mike
Back to top
View user's profile Send private message Visit poster's website
kilikopele
LXF regular


Joined: Wed Aug 09, 2006 4:35 pm
Posts: 298

PostPosted: Thu Nov 16, 2006 3:35 am    Post subject: RE: Assembly problems (can Reply with quote

Good answer, thanks. So are there routines in the current Linux kernel written to or converted to assembler? I thought it was all C.

Is the code produced by HLA roughly the same code assembled by NASM, MASM, or any other ASM?
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Thu Nov 16, 2006 10:08 am    Post subject: RE: Assembly problems (can Reply with quote

There is a fair sprinkling of .S files in the sources. And the ../include/asm headers as well, of course.

If you compile that 'hello world' program passing gcc the '-s' switch rather than a '-o', you should get a good example of the Linux equivalent.


jm
_________________
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Thu Nov 16, 2006 10:30 am    Post subject: RE: Assembly problems (can Reply with quote

There are quite a few kernel routines written in assembler -- look for filenames with .s or .S extensions in the kernel source directories. Some of them are in asm simply because they do things that C can't (eg setting the stack pointer) and some are there to wring the maximum speed out of code that's called thousands of times a second. But 98-99% of the kernel is written in C.

I've never used HLA, so I'm afraid I don't know. I don't really see the point of HLA to be honest -- adding BASIC-like coding methods to assembler just seems to make the worst of both worlds! And then the object and classes stuff... Assembler is all about talking directly to the CPU, and that's what makes it fascinating (and hard). If you're going to add a load of high-level syntactical fluff around it, you may as well just use C++ and co., IMHO.

Mike
Back to top
View user's profile Send private message Visit poster's website
kilikopele
LXF regular


Joined: Wed Aug 09, 2006 4:35 pm
Posts: 298

PostPosted: Sat Nov 18, 2006 5:02 am    Post subject: RE: Assembly problems (can Reply with quote

Thanks. Some great answers and I guess I have my .s homework cut out for me. One final question: Is there a decent primer on assembler that, used in conjuction maybe with viewing some real code examples as mentioned above, would make sense to someone familiar with C/C++?

I don't want to code the next Menuet OS. I just want to gain a bit of a better understanding.
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Sat Nov 18, 2006 2:52 pm    Post subject: RE: Assembly problems (can Reply with quote

I recommend reading through this, up until (and including) the "It's All About The Registers" part:

http://www.asmcommunity.net/x86book/index.php?title=Main_Page

Then move on to:

http://asm.sourceforge.net/intro/Assembly-Intro.html

Which deals with writing and running small assembler programs under Linux. Who knows... maybe there'll be a guide in a future LXF? Wink

PS - Incidentally, what sounds better -- "assembler" or "assembly"? Paul reckons the latter, and he's technically right, but I just find it weird to say. "It's written in assembler" vs "It's written in assembly"...?

Mike
Back to top
View user's profile Send private message Visit poster's website
SIGSEGV



Joined: Sat Apr 29, 2006 5:20 pm
Posts: 41
Location: Almere, Netherlands

PostPosted: Sat Nov 18, 2006 11:25 pm    Post subject: Re: RE: Assembly problems (can Reply with quote

M-Saunders wrote:
PS - Incidentally, what sounds better -- "assembler" or "assembly"? Paul reckons the latter, and he's technically right, but I just find it weird to say. "It's written in assembler" vs "It's written in assembly"...?

Mike

Afaik, the language is called Assembler and a piece of code written in that language would be called assembly.. So it's "assembler" and "an assembly", and "an assembler" would be one who assembles.. But English is not my native language, so perhaps i'm posting utter nonsence here.. Wink
Back to top
View user's profile Send private message
kilikopele
LXF regular


Joined: Wed Aug 09, 2006 4:35 pm
Posts: 298

PostPosted: Tue Nov 21, 2006 2:41 am    Post subject: RE: Re: RE: Assembly problems (can Reply with quote

Doesn't sound like non-sense to me. About all I can say is that I see them interchanged so much as make any rule unenforceable.

Gee. "If" a guide comes out in a future issue, direct all praise for an interesting and useful tutorial back to me. All criticism towards a tutorial on such an untouchable or difficult subject should be directed to /dev/null. Smile
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Tue Nov 21, 2006 9:51 am    Post subject: RE: Re: RE: Assembly problems (can Reply with quote

>>
PS - Incidentally, what sounds better -- "assembler" or "assembly"? Paul reckons the latter, and he's technically right, but I just find it weird to say. "It's written in assembler" vs "It's written in assembly"...?
>>

(grin)

I always pronounce it with a 'bell' part ... the first is the action ...

Just be glad we have 'C' Smile

And not just becourse it's easier to spell either. (it does help though)
_________________
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Tue Nov 21, 2006 2:02 pm    Post subject: RE: Re: RE: Assembly problems (can Reply with quote

Thinking about this its obvious how mad what I suggested is. I assume that Linux and DOS hook the IVT up differently so trying to call INT 21h in Linux was obviously going to do something random or nothing at all. So all the basic MOV instructions and such would work fine but the interrupts are naturally different.

What we need is POSIX for IVTs so that I can assemble in peace Smile . (obviously DOS would have to use it as well)

Out of interest. If character i/o is handled via a machine code call to the IVT and the IVT redirects to (I assume) yet more machine code then how do you handle i/o directly in ASM (i.e. what code is the IVT calling). I know that actual output in hardware would be handled via the data, address and control buses but how are they utilised in ASM in such a case.
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Tue Nov 21, 2006 2:23 pm    Post subject: RE: Re: RE: Assembly problems (can Reply with quote

Indeed. And it's not just the interrupt numbers that are different; the parameters you pass vary too. In DOS, for instance, you put 9 in AX to print a string, whereas in Linux you put 4 in AX.

And to make things more complex, the calling convention differs between UNIX flavours! In Linux, you store int 80h call parameters in registers (EAX, EBX etc.) whereas in FreeBSD you push the parameters onto the stack before calling int 80h. Ker-azy!

As your your second question: in a non-protected mode OS like DOS, you can access the hardware directly if need be. For instance, you can mov data into the video RAM if you want direct access. So you can avoid interrupts and the IVT, and go directly to hardware devices if need be.

Under Linux, though, that's not possible -- normal programs can't access memory outside of their segments, so a user program can't suddenly start writing data to the video memory. Hence why the X server needs to be run as root. It's a security and stability measure but limits what your code can do.

I like your idea of a POSIX for interrupts Smile

Mike
Back to top
View user's profile Send private message Visit poster's website
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast