News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

x86 Assembler on windows...how to start?

Started by falcon01, July 28, 2011, 03:32:03 PM

Previous topic - Next topic

falcon01

Hi, I already had some experience with assembly programming for motorola68k,mips and intel 8086, everything went well because I had every simulator/tool I needed + readable guides.

Now I'm trying to learn x86 but
1)I can't find any simulator (I want to execute my code step by step, view registers/memory,things like that...with a gui would be the best)
2)I can't assemble!I tried masm but...every guide I find starts in a different way! One tells me to write a c program, another starts with a segment directive, another writes directly .data and .code, another one switches directly to code...

So, out of desperation, I beg for your help!
Can you give me tools and write here a small tutorial in order to build a simple "hello world" program?

baltoro

A great place to start is: Hutch's Home Page
And, as for beginner's tutorials,...most of us started with: Iczelion's Example Code
Have fun here,...It's really a great forum.
Baltoro

falcon01

Thanks but...it's not different from what I already found on the net...iczelion just gives some code away, I'd like to have a "how to build and run your x86 assembly code" guide, I already know something about architecture, registers, op codes, ecc. it's just I can't get a program run!
For example I tried to make this code but it does not work:

.386                        ;I added this looking at iczelion but it does assemble even without it
.model small
.stack 1024

.data
MyMessage      db "Hello x86!$"

.code
start:
lea bx,MyMessage
mov dx,offset MyMessage
mov ah,9
int 21h            ;prints the string MyMessage on the console
return
end start


I did this in order to compile:
Quote1. masm hello.asm
2. link hello.obj

Then I open my command line, write "hello.exe" and...no writing at all! Why?I'm sure the system call is correct...

jj2007

My tips & tricks page has a Hello World example and step by step instructions. Iczelion's tutorial should come once you get bored of Hello World.

Welcome to the Forum :thumbu

falcon01

Thanks to you too, for help and welcome, but I'd like to not use any macro like in your example (masm32rt.inc is a macro file right?) ... don't you have a simple print on console (with system call) example?Or maybe try my program and suggest me what's wrong with it?
I think my level of assembler will stay confined on command line, I don't plan going so far like making a gui for my programs.

BogdanOntanu

Your program uses int21h ... it is an old DOS kind of 16 bits program...

Try to forget about this kind of obsolete ASM examples and immerse your self into 32 bits windows programming (can be console and not gui if you so like).

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

jj2007

Bogdan is right, forget 16-bit code. Here is a 32-bit Hello World example, for testing if your installation works:

include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0

start: MsgBox 0, "Hello World", addr AppName, MB_OK
exit

end start


As to macros: I understand that you want the "pure" code, but nothing is pure nowadays. Even if you replace print "hello world" with the full invoke StdOut etc stuff, you are using a third party code. In this context, \masm32\macros\macros.asm is a fascinating lecture :bg

FORTRANS

Hi,

   The masm32rt.inc is an include file that gived you access
to a run-time library (or DLL).  You can look at 16-bit programs
in DEBUG.EXE.  That will let you see the register contents.
Your program is not exiting correctly.  An *.EXE program should
use function 4C to exit to DOS.  You should use an input character
function to pause the program so you can see the message if
Windows is closing your program too quickly.

   I don't use the .MODEL directive, so this may be extra but
won't hurt.  You did not initialize the data segment.


; Put these two lines as the first lines in your program
        MOV     AX,SEG DGROUP
        MOV     DS,AX


HTH,

Steve N.

P.S.
Just saw Bogdan and JJ respond.  This forum is intended
mostly for 32-bit Windows code using the MASM32 package.

SRN

dedndave

he obviously does not have masm32 installed
he used "link" to create an MZ EXE   :bg

step 1) install the masm32 package
the link is in the upper right corner of the forum

jj2007

Quote from: dedndave on July 28, 2011, 05:23:55 PM
he obviously does not have masm32 installed

Again, see my signature for step by step instructions. By the way, your "emulator" is called Olly - see http://www.ollydbg.de/version2.html

mineiro

Hello falcon01, welcome. Well, I'm putting here your question, but follow other peoples sugestions, don't have fear, programming to windows is more easy than to ms-dos. If you like to talk about ms-dos, this forum have a special section to this, look in front page.
Quotegrupo group seg_cod,seg_dados
assume cs:grupo,ds:grupo

seg_cod segment public
org 100h

imp_string proc far
mov ah,9
mov dx,offset grupo:string
int 21h
int 20h
imp_string endp

seg_cod ends

seg_dados segment public
string db "Hello x86!$"
seg_dados ends

end imp_string

Quote
masm hello.asm
link hello.obj
exe2bin hello.exe hello.com
Oh, I forgot to say, you can use "emu 8086" too, search by it on net.

falcon01

Ok I installed masm32 and it gives me this error:
Quotemov dx,offset Mystring <--error A2022: instruction operands must be the same size
maybe it's for this reason my program does not work!What's its meaning?

2 mineiro:
your code works fine, could you help me to understand it?It's pretty much the same as mine except for the fact you use segments and no directives as .model,.data e .code...what's the difference?
Moreover I like 8086emu much (I used it for 8086 assembly indeed), but I need x86 programming...too bad I'll have to use ollydb:(
2 fortrans:
even with the exit function (thanks for the hint anyway!) it still does not work!
2 everyone:
I don't really mind programming in windows or MS DOS, as long I can make command line programs...the thing is I don't want message boxer or any sign of gui...

baltoro

Offests are 32-bit values, so, use the full register (EDX) instead of the 16-bit portion.
mov edx, offset Mystring

This MASM forum thread: INVOKE and ADDR Directives is an interesting discussion of the actual functions of the ADDR and OFFSET directives.   

The reason the guys are showing you code that uses the MessageBox function is that it's a standard Windows API function. It's there primarily to demonstrate the INVOKE syntax, which is a very common and useful technique in MASM programming. Of course, you can accomplish essentially the same thing without using the MessageBox finction.

You can use the search function for the MASM forum, and, you will discover all kinds of great threads about subjects that you have not yet asked, but, other novice assembly programmers have. Here's what I do: I click on the Advanced Search link, and enter "dedndave" into the, "By User" text box,...then deselect all forums, and, re-select just the Campus, Workshop, and, Laboratory sub-forums. Supply keywords for the "Search For" text box (subject you are interested in), or, an asterisk (*) to select all. Dave posts all kinds of useful demo code. It's clear, it works, and, he explains everything in great detail. I'd still be a really crappy programmer, if it wasn't for Dave.

Here's a short example program that I stole from Dave:   

        INCLUDE \masm32\include\masm32rt.inc

        .DATA

Hello   db 'Hello World!'

        .DATA?

ByteCnt dd ?

        .CODE

_main   PROC

        INVOKE  GetStdHandle,STD_OUTPUT_HANDLE
        INVOKE  WriteFile,eax,offset Hello,sizeof Hello,offset ByteCnt,NULL
        INVOKE  ExitProcess,0

_main   ENDP

        END     _main
 

Baltoro

falcon01

Thanks much the error is fixed but...program still doesn't work!
And I discovered another thing: mineiro program does work as long I execute the .com file, the .exe crashes!
Ah by the way I'm assembling mineiro's program with MASM5.0, if I try assembling it with MASM32 it gives some errors...I'm still far away from the solutionT_T

I'll try to see invoke functions, though I love explicit interrupts more...

dedndave

the EXE crashes because the DS segment register is not set
for COM programs, CS, DS, ES, SS all point to the same segment

now, if your having trouble with the 32-bit example, it may be because you are not using the correct command line switches for ML and/or LINK
ml /c /Fl /coff MyProg.asm
link /SUBSYSTEM:CONSOLE /OPT:NOREF MyProg.obj


be sure you are using the link that is in masm32\bin for 32-bit programs
the one that came with masm 5.0 is for 16-bit programs
you may have to check the PATH environment variable, too

the /Fl switch is not necessary, but it may be used to generate a listing

QuoteI'll try to see invoke functions, though I love explicit interrupts more
as far as i know, you can't make a GUI window with interrupts   :P
at least, not easily - lol