Hi, im new here :)

I'm trying to get into assembly programming, so to start off I want to make a program that reads a text file and displays the output. I've already written a hello world program, and have a pretty good understanding of the basics of assembly.

(I'm using the gas assembler which is AT&T syntax)

1) I don't know how to read the file. I know I need to read (syscall 3) and write (syscall 4). What I'm stuck on is what do I use for *buf?
ssize_t read(int fd, void *buf, size_t count);
Would I use an .ascii and read some of the file, write it to stdout, and continue looping until end of file(I think when read returns 0)?

2) Where is a listing of mnemonics? Like mov, add, etc and their meanings?

Thank you !
--monkey
Posted on 2004-02-07 15:29:24 by ninmonkey
You sound confused with some *nix-based OS.
In Windows you would use the CreateFile()/ReadFile()/CloseHandle() APIs, etc.
The buf variable is ofcourse a pointer to the buffer that the data should be read into.

For a list of mnemonics, download the Intel Instruction Reference manual from their website somewhere (in PDF form).

And I would suggest learning a simpler language first, and master the art of programming itself, so your only questions will be asm-related, rather than "how do I read a file?".
Posted on 2004-02-07 16:52:44 by Henk-Jan
Yeah, maybe this thread needs to be moved to Heap so that syscall stuff can be discussed. :)

Anyhow, as for the first question, how do you do that in C? If you know it, it is a simple matter of translation. Really a no-brainer even for a newbie. And if you use GAS, you _have to_ read gas manual, because you cannot directly copy and paste Intel manual stuff.
Posted on 2004-02-08 00:34:36 by Starless
Hi Ninmonkey,

Seems like you are indeed a newbie to assembler programming. My suggestion is you start reading some tut's concerning the subject. To start out learning the basics of asm-mnemonics take a peek at this site:
http://exagone.cjb.net
it covers basic info about mnemonics including examples of their use (just download the html-help in the tutorial-section).
And off course there is the 'queen' of tutorials on Win32asm programming, Iczelions great tutorials on windows programming in assembler. You 'll find them here:
http://win32asm.cjb.net

And remeber: At first it 'll feel like you are learning Swahilli or something, but once you get the feel for it, it 'll be as easy as drinking a glass of milk! (meaning: don't give up!, the start is the hardest part!)

Just another tip: As you are just starting out, why not concider to use another assembler in combination with an assembler IDE. I personally use Masm (because of its pseudo-high-level-code) and Winasm-IDE. It makes things just so much more comfortable! You 'll find plenty of info about Masm and Winasm on this board. you can also check out these urls:

www.movsd.com
www.code4u.net/winasm/index.html
(off course this is a personal choice, jus check out whitch assembler and/or IDE fits you most).


Finally I suggest you get yourself a copy of the Windows API refference (check the board). A must for win32asm-programming!

Wish you the best in asm-programming,

Friendly greetings,

Lovesong.
Posted on 2004-02-08 03:32:26 by lovesong
Just question, are you programming for linux or windows?
Posted on 2004-02-08 04:10:13 by roticv
roticv: sorry about the confusion :P I forgot to say this was for linux. But, I was wondering, if I use libc and standard c functions, would a program be pretty easily compiled for linux and windows? (I have gcc, ld, as, etc., the needed binaries to compile from windows)

Henk-Jan: I do know other languages pretty well. I can easily do this in others, but I want to learn assembly.

The part that is confusing is I can't find any basic examples on datatypes/how ever you handle them in assembly. Take the .ascii/.string for example, would I be able to modify their sizes/data during runtime? Are they like c-strings? (char arrays) or like std::string?(c++ strings) or something else?

Starless: I have the gnu gas manual so I'm okay there :)

So basically
a) Do you know about assembly+libc on windows and linux or have any usefull links?
b) What datatype would I use to store what read(), reads?

Thanks
Posted on 2004-02-08 11:06:43 by ninmonkey
Henk-Jan: I do know other languages pretty well. I can easily do this in others, but I want to learn assembly.


You obviously can't do it in the language that matters though: C.
If you know C, asm is just a formality, when it comes to calling API functions. Mind you, one should ask himself why he would use asm at all in such a case. Especially if you are using the C library.

What you need is a book on computer organization. Learn how the computer (CPU + memory) works, then asm is simple. You could try reading the first chapters of Art of Assembly, they deal with this subject. You can find it somewhere on the web for free download, google is your friend.
Posted on 2004-02-08 11:22:44 by Henk-Jan
Henk-Jan, all: Here's what I have so far, does this look right If i'm reading from stdin?
/* reads from stdin */

.data

buf:
.byte

.text
.global _start

_start:

movl $3,%eax /* syscall read */
movl $0,%ebx /* stdin, buf, 1 byte */
movl $buf,%ecx
movl $1,%edx
int $0x80

/* if return == 0, then EOF, jump to quit */
cmp $0,%eax
je _quit

/*and print*/

movl $4,%eax /* sys write */
movl $1,%ebx /* stdout, buf, 1 byte */
movl $buf,%ecx
movl $1,%edx
int $0x80

/* read again */
jmp _start

/*quit*/
_quit:

movl $1,%eax /* sys_exit */
movl $0,%ebx /* return 0 */
int $0x80
Posted on 2004-02-08 17:00:15 by ninmonkey