Author Topic: Just getting started  (Read 16814 times)

Offline Flippin Monkey

  • New Member
  • Posts: 1
Just getting started
« on: June 23, 2010, 04:33:52 AM »
I'm sorry you guys have probably ran into countless posts like these (it would help if a good answer to them were sicked).
I am just starting assembly, I've got some very very basic concepts down, and I'm coming from a history of c++ mainly, java secondary, and c#.

Right now I'm running a dell computer with windows vista and Linux ubuntu 10.4 (I don't use linux, but I'm trying to learn)
Point being, I am looking for a way to do assembly cross platform that way when I am more into Linux I don't have to start this whole process over again.

So lets get started,

I figured NASM would be a great assembler, it seems like a pretty good compiler and it's syntax is not too bad, (if I should be using something else let me know)
I am also using minGW32 for linking.

Here is my batch file for building:
"C:\Program Files\NASM\nasm" -f win32 program.asm -o program.obj

and my batch for linking
"C:\MinGW\bin\gcc.exe" program.obj -o program.exe

then i attempt to run the program from that directory using program.exe

However, whenever I try to do basically anything but use registers the program crashes.

Here is my non crashing program:
Code: [Select]
segment .text
global _main

_main:
mov al, 0
mov ah, 0
mov ax, 0
mov eax, 0
ret

segment .data

and my crashing program:
Code: [Select]
segment .text
global _main

_main:
mov al, 0
mov ah, 0
mov ax, 0
mov eax, 0

mov al, 2
mov ah, '!'
int 21h

ret

segment .data

I've tried different ways of assembling, linking etc, i've probably gone through about 10 assemblers and linkers combined. I havent even got a chance to read my book yet because I can't follow along because my code won't run correctly.

Any help would be much appreciated, thanks in advance

Offline michaeljreeves

  • Jr. Member
  • *
  • Posts: 3
Re: Just getting started
« Reply #1 on: June 23, 2010, 06:11:32 PM »
Obviously using > int 21h < is causing the crash.

I don't remember off the top of my head what the interrupt references, keyboard, hdd or something.

BUT you aren't writing/calling to it or reading its status.

Guess you better read your books about how to reference an interrupt and use it in a program.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Just getting started
« Reply #2 on: June 23, 2010, 06:17:34 PM »
You're trying to read a random record from an opened FCB? What is this, "old code day"? :)

The problem is that int 21h is dos (16-bit), and you're assembling and linking as a Windows PE (32-bit) executable. Won't work.

If you assemble it into a .com file (nasm -f bin -o myfile.com myfile.asm - no linker needed) it may or may not work. I think 32-bit Vista supports dos ("fake dos"), 64-bit Vista does not(?). Won't work on Linux... except under dosemu (try "sudo aptget dosemu install" in Ubuntu - no guarantee that's right).

We can show you a simple example of a Windows PE and/or an ELF executable for Linux. In the "example code" section of this forum there's a "cross-compiling" example that might interest you. But that won't help you follow along with your book, which is apparently for dos(?). What book are you using, Flippin Monkey? (I'm probably not familiar with it, but it won't hurt to try)

A simple example of a dos .com file:

Code: [Select]
; nasm -f bin -o myfile.com myfile.asm

org 100h ; dos will load us at offset 100h (256) into some segment

section .text

    mov ah, 9
    mov dx, msg
    int 21h

    ret

section .data
    msg db "hello world!$"


A dos .com file is about as simple as it gets. Without knowing, I'd guess that your book is probably teaching you to create an MZ executable - only slightly more complicated, but requires slightly different source code (and a linker - http://alink.sf.net should work well with Nasm). We can provide an example of that, too. Your book presumably has examples, but they may not be in the proper syntax for Nasm. Easy enough to "translate" (we can help), but it may be easier to use the assembler that your book expects. As you've probably figured out, assemblers "do" the same thing, but have different syntax... and generally won't assemble each other's code. :(

To be honest, I'd jump right into Linux, if I were you. It's "different", but it isn't really that "scary". For comparison:

Code: [Select]
; nasm -f elf32 myfile.asm
; ld -o myfile myfile.o

global _start  ; tell the linker

section .text

_start:
    mov edx, msg_len  ; length to write
    mov ecx, msg        ; address of buffer
    mov ebx, 1            ; "file descriptor" - stdout
    mov eax, 4            ; write to a file
    int 80h

    mov eax, 1            ; exit to shell
    int 80h

section .data
    msg db "hello world", 10
    msg_len equ  $ - msg  ; let Nasm count 'em!


Simple enough, eh? But it probably won't help you follow along in your book. Perhaps you should post an example from the book, and we can show you what changes (if any) would make it work with Nasm.

Nasm *is* a great assembler - you got that part right! :)

Best,
Frank