Assembly problems (can't get helloworld working).

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Assembly problems (can't get helloworld working).

Postby GMorgan » Sat Nov 11, 2006 5:09 pm

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

Code: Select all
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.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: Assembly problems (can

Postby M-Saunders » Mon Nov 13, 2006 10:02 am

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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

RE: Assembly problems (can

Postby GMorgan » Tue Nov 14, 2006 3:55 pm

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.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: Assembly problems (can

Postby kilikopele » Wed Nov 15, 2006 3:20 am

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++?
User avatar
kilikopele
LXF regular
 
Posts: 298
Joined: Wed Aug 09, 2006 3:35 pm

RE: Assembly problems (can

Postby M-Saunders » Wed Nov 15, 2006 10:32 am

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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

RE: Assembly problems (can

Postby kilikopele » Thu Nov 16, 2006 3:35 am

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?
User avatar
kilikopele
LXF regular
 
Posts: 298
Joined: Wed Aug 09, 2006 3:35 pm

RE: Assembly problems (can

Postby jjmac » Thu Nov 16, 2006 10:08 am

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 ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

RE: Assembly problems (can

Postby M-Saunders » Thu Nov 16, 2006 10:30 am

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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

RE: Assembly problems (can

Postby kilikopele » Sat Nov 18, 2006 5:02 am

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.
User avatar
kilikopele
LXF regular
 
Posts: 298
Joined: Wed Aug 09, 2006 3:35 pm

RE: Assembly problems (can

Postby M-Saunders » Sat Nov 18, 2006 2:52 pm

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

http://www.asmcommunity.net/x86book/ind ... =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? ;-)

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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Re: RE: Assembly problems (can

Postby SIGSEGV » Sat Nov 18, 2006 11:25 pm

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.. ;)
User avatar
SIGSEGV
 
Posts: 41
Joined: Sat Apr 29, 2006 4:20 pm
Location: Almere, Netherlands

RE: Re: RE: Assembly problems (can

Postby kilikopele » Tue Nov 21, 2006 2:41 am

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. :)
User avatar
kilikopele
LXF regular
 
Posts: 298
Joined: Wed Aug 09, 2006 3:35 pm

RE: Re: RE: Assembly problems (can

Postby jjmac » Tue Nov 21, 2006 9:51 am

>>
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' :)

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 ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

RE: Re: RE: Assembly problems (can

Postby GMorgan » Tue Nov 21, 2006 2:02 pm

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 :) . (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.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: Re: RE: Assembly problems (can

Postby M-Saunders » Tue Nov 21, 2006 2:23 pm

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 :-)

Mike
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Next

Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests