flat assembler
Message board for the users of flat assembler.

Index > Main > code compile problem

Author
Thread Post new topic Reply to topic
MaaSTaaR



Joined: 09 Aug 2005
Posts: 8
Location: Kuwait
MaaSTaaR
Hello Smile

i am new with Assembly , now i study it from http://www.xs4all.nl/~smit/asm01001.htm , i try to compile this code with flat assembler

Code:
;-- -----------------------------------------------------------------------
;- Written by Ferdi Smit for use with the "ASM Tutorial Part 1"          -
;-------------------------------------------------------------------------
; To compile:   TASM examp1.asm                                             -
;               TLINK examp1.obj                                            -
;----------------------------------------------------------------------------
.model small
.stack
.data
message   db "Hello world, I'm learning Assembly !!!", "$"

.code

main   proc
   mov   ax,seg message
   mov   ds,ax

   mov   ah,09
   lea   dx,message
   int   21h

   mov   ax,4c00h
   int   21h
main   endp
end main
    


but it's show this error "illegal instruction" when i try to compile it Smile
Post 09 Aug 2005, 07:41
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
2nd argument of LEA must be memory address (lea stands for load effective address), so "lea reg,[anything]" works like "mov reg,anything", but in case of lea you have more possiblities for "anything", like "lea eax,[4*eax + eax]" (eax = eax*5) etc...
so i quess your problem is "lea dx,message", should be "lea dx,[message]", or preferably "mov dx,message", no reason to LEA if you don't know what you are doing....


Last edited by vid on 09 Aug 2005, 13:46; edited 1 time in total
Post 09 Aug 2005, 07:53
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Thank you Vid, for that succinct and lucid clarification. I would only add, to maastaar, that in general one should select instructions, ESPECIALLY when first writing programs (NOT CODING), that are as SIMPLE as possible. There is no substitute for clarity and readability. The author of your site:
http://www.xs4all.nl/~smit/asm01001.htm
has done a fine job. HOWEVER, with regard to his statement:

"Well, I'll start with the basics, like instruction format and some simple instructions to manipulate registers. I don't know how much you know about coding, so I'll explain even the most simple stuff. Please note, that I explain 8086 assembly coding. That means NO 32-bit registers and instructions and NO protected-, real- and virtual 86 mode for now...."

I would submit that it is MORE DIFFICULT for debutants to commence with real mode "coding", than with protected mode PROGRAMMING, in which all segments disappear, (i.e. all assigned the same value), so as to make assembly language PROGRAMMING just like programming in any other language. I am sure many folks on this forum would dispute my point of view! Smile Rolling Eyes
Post 09 Aug 2005, 09:28
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
Hi maaSTaar,

This bit of code has to be rewritten in FASM syntax in order to work:

Code:
;------------------------------------------------------------------------- 
;- Written by Ferdi Smit for use with the "ASM Tutorial Part 1"          - 
;-------------------------------------------------------------------------
;- ReWritten for FASM by -smiddy                                         - 
;-------------------------------------------------------------------------
;- To Compile: FASM Hello.asm                                            -
;-------------------------------------------------------------------------

   format  MZ                      ; For FASM EXE format


main:

   mov   ax,cs                                          ; Move CS into AX 
   mov   ds,ax                                          ; Put CS into DS

   mov   ah,09                                          ; Function 9 from DOS
   lea   dx,[message]                           ; Load effective address into DX 
   int   21h                                            ; Call DOS functions

   mov   ax,4c00h                                       ; Function 4C00h (exit to DOS)
   int   21h                                            ; Call DOS functions
   
message   db "Hello world, I'm learning Assembly !!!", "$" 

    


Getting this to compile:

Code:
C:\smiddy>fasm hello.asm
flat assembler  version 1.58
2 passes, 88 bytes.

C:\smiddy>hello
Hello world, I'm learning Assembly !!!
C:\smiddy>
    


Instead ot the
Code:
. . . 
LEA DX,[message]
. . . 
    


most people use:

Code:
. . . 
MOV DX,message
. . .
    


I hope this helps a little. It will take a little time in order to make the jump to FASM, but to me it is very rewarding!
Post 09 Aug 2005, 10:13
View user's profile Send private message Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius
I see your tutorial used MASM or TASM syntax. If you want to use FASM you should find a tutorial that uses FASM syntax. There aren't too many, but since you're using DOS you can luckily try the TAJGA tutorial: http://www.decard.net/article.php?body=tajga

BTW, using LEA where MOV can be used is something a lot of old assembly books used. I guess they thought it was more readable, or maybe because you had to use the OFFSET keyword with MOV in MASM (There is no OFFSET keyword in FASM, you use the name of the variable to find the offset and the variable in "[" "]" to get the value).

Anyways, good luck learning assembly!
Post 09 Aug 2005, 17:51
View user's profile Send private message AIM Address Reply with quote
MaaSTaaR



Joined: 09 Aug 2005
Posts: 8
Location: Kuwait
MaaSTaaR
Hello ..

thanks very much for all Smile

it's work now but i have question

why we change

Code:
   mov   ax,seg message 
    


to

Code:
   mov   ax,cs 
    


and why we copy CS value to AX ?

now i am study from this http://www.decard.net/article.php?body=tajga , thank you very much THEWizardGenius Rolling Eyes


this is my first code , is this great work ? Smile

Code:
;=======================
; Codded by MaaSTaaR Wink
;=======================

org      256    ; Start

mov     ah,2    ; Give ah value 2 to print dl value

mov     dl,'H'
int     21h     ; Print dl value
mov     dl,'e'
int     21h     ; Print dl value
mov     dl,'l'
int     21h     ; Print dl value
mov     dl,'l'
int     21h     ; Print dl value
mov     dl,'o'
int     21h     ; Print dl value
mov     dl,' '
int     21h     ; Print dl value
mov     dl,'A'
int     21h     ; Print dl value
mov     dl,'S'
int     21h     ; Print dl value
mov     dl,'M'
int     21h     ; Print dl value
mov     dl,'!'
int     21h     ; Print dl value


int     20h     ; The end Smile

; Is this good work ? Smile
    
Post 11 Aug 2005, 13:47
View user's profile Send private message Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius
CS tells which segment the code is located. "seg message" is MASM/TASM for "the segment that the variable message is located in". FASM has no such word. However, since your data and code are in the same segment, CS is the same. If you had data and code in different segments, the data would probably be in DS (which tells which segment the data is in, when data and code are in different segments) so you would type
Code:
mov ax, ds
    

to do it that way.

Your code is good, but too complex. You print one character at a time. You can write the whole string at once. Use INT 21h with AH=9 to do this. A reference about the DOS system calls, http://spike.scu.edu.au/~barry/interrupts.html#ah09 says this:
Quote:

AH = 09h - WRITE STRING TO STANDARD OUTPUT
Entry: DS:DX -> '$'-terminated string
Return: AL = 24h
Notes: ^C/^Break are checked
SeeAlso: AH=02h,AH=06h"OUTPUT"

So you copy CS to DS (since the data is in CS) and DX points at the string. Then put 9 into AH and call INT 21h. Remember, the string must end with "$" or DOS will just keep printing gibberish until it crashes or something. Here's example code that does the same thing as your code:
Code:
mov dx, hwstring
;You can't do "mov ds, cs" so you have to do it in two steps.
mov ax, cs
mov ds, ax
mov ah, 9
int 21h
int 20h

hwstring db 'Hello, ASM!$'
    


Much better, right? But that's OK, you didn't know. So what happens is when you call INT 21h and AH=9, DOS starts at DS:DX and starts checking each character. If the character is "$" it stops. If not, it prints the character and checks the next one. What if you have to have a "$" in your string, such as "Only costs is $19.99!!!"? Then you print the first part of the string, then print a "$" with AH=2, then print the rest of the string:
Code:
;Copy CS to DS
mov ax, cs
mov ds, ax
mov dx, coststringa
mov ah, 9
int 21h
;Now print a single character
mov ah, 2
mov dl, '$'
int 21h
mov dx, coststringb
mov ah, 9
int 21h
int 20h

;Note that INT 21h AH=9 does NOT print the "$", we have to do that
;with INT 21h AH=2 in between the two strings. Thus the strings
;must be split into two.
coststringa db 'Only costs is $'    ;Stops at "$", does not print "$"
coststringb db '19.99!!!$'
    

Do you understand? Now if you don't know what a segment is, tell me. I can tell you.
Post 11 Aug 2005, 17:23
View user's profile Send private message AIM Address Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7725
Location: Kraków, Poland
Tomasz Grysztar
THEWizardGenius: actually to code it more analogously to TASM version it would be better to do:
Code:
format MZ
entry codeseg:main

segment dataseg

message db "Hello world, I'm learning Assembly !!!", "$"

segment codeseg

main:

   mov   ax,dataseg
   mov   ds,ax

   mov   ah,09
   mov   dx,message
   int   21h

   mov   ax,4c00h
   int   21h    
Post 11 Aug 2005, 17:37
View user's profile Send private message Visit poster's website Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
You can also use function 40h to print the string, it can also print dollar signs and print to files instead of the screen.

Check this link:
http://www.uv.tietgen.dk/staff/mlha/PC/Prog/asm/INT/index.htm

To learn assembly for DOS, you should be familiar with BIOS interrupts and DOS functions.

Code:
format  MZ 
entry codeseg:main 

segment dataseg 

;In MS-DOS, the 13,10 byte sequence means "change line"
message db "Hello world, I'm learning Assembly !!!", 13, 10, "Look, I can print $$$$ signs !!!"

;in Fasm syntax, $ means "this location", so this returns the lengh of the string
lenght  = $-message 

segment codeseg

main: 

   mov   ax,dataseg 
   mov   ds,ax 

   mov   ah,40h
   mov   bx,1           ;handle of standard output
   mov   cx,lenght
   mov   dx,message 
   int   21h 

   mov   ax,4c00h 
   int   21h
    
Post 11 Aug 2005, 18:12
View user's profile Send private message Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius
I don't know TASM so I did it the FASM way. Since he's apparently trying to use FASM, that makes more sense anyways.

El Tangas: that page doesn't tell about INT 21h, but it links to this page (which does): http://www.uv.tietgen.dk/staff/mlha/PC/Prog/asm/INT/21/index.htm
Post 12 Aug 2005, 01:21
View user's profile Send private message AIM Address Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
anyway int 21h/ah=9 is described further in tutorial
Post 15 Aug 2005, 09:49
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
zoijar



Joined: 22 Aug 2005
Posts: 2
zoijar
tom tobias wrote:
Thank you Vid, for that succinct and lucid clarification. I would only add, to maastaar, that in general one should select instructions, ESPECIALLY when first writing programs (NOT CODING), that are as SIMPLE as possible. There is no substitute for clarity and readability. The author of your site:
http://www.xs4all.nl/~smit/asm01001.htm
has done a fine job. HOWEVER, with regard to his statement:

"Well, I'll start with the basics, like instruction format and some simple instructions to manipulate registers. I don't know how much you know about coding, so I'll explain even the most simple stuff. Please note, that I explain 8086 assembly coding. That means NO 32-bit registers and instructions and NO protected-, real- and virtual 86 mode for now...."

I would submit that it is MORE DIFFICULT for debutants to commence with real mode "coding", than with protected mode PROGRAMMING, in which all segments disappear, (i.e. all assigned the same value), so as to make assembly language PROGRAMMING just like programming in any other language. I am sure many folks on this forum would dispute my point of view! Smile Rolling Eyes

I stumbled on this topic via a google on my name Smile I'm in fact the author of these tutorials, ferdi smit, and I just had to respond to this.

While I fully agree with you _now_, this wasn't the case when I wrote these texts. One should note that I wrote most of this in 1995-1996, a time when protected mode was fairly uncommon and new (and I was only 15-16 years old, and not a phd student yet...quite a change Wink ). I remember having to do my own protected mode setup in assembler, and tricking the compiler into accepting some specific things. Protected mode was much more complicated back then, simply because compilers couldn't understand it yet, and there was no standard support. That being said, a tutorial introduction should not needlessly complicate things, and therfore I chose realmode. This only shows how outdated these tutorials actually are Smile I toyed with the idea of removing them from the net completely very often, but I still get positive feedback from thankful readers, even now. So...there is no harm in keeping them online.

For the topic starter: while my tutorials seem to be easy to understand for many people, and a decent introduction, I would still urge you to buy a modern book on assembler programming: 1996 is ancient history in the world of computing...
Post 22 Aug 2005, 18:04
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
to zoijar
1. why buy? there are many good online
2. I think starting from beginning is good thing. modern ways start with prot. mode ring 3 programming (win32/linux) which causes people just don't understand many things and also they don't understand reason of changes in histrocial context
Post 22 Aug 2005, 21:02
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
zoijar



Joined: 22 Aug 2005
Posts: 2
zoijar
vid wrote:
to zoijar
1. why buy? there are many good online
2. I think starting from beginning is good thing. modern ways start with prot. mode ring 3 programming (win32/linux) which causes people just don't understand many things and also they don't understand reason of changes in histrocial context

1 - Because it's better to learn from a consistent, coherent whole, than from bits and pieces scrapped from the net.

2 - You're partially correct: the understanding of historical changes is nice. However, none of the modern operating systems really allow you to work in realmode (I'm not sure about virtual 8086 mode). So you would need an old DOS partition or something similar. Besides that most people want to learn something that can be used now. However, learning the historical context and evolution of hardware can be a significant insight; it sure helped me a great deal in university... C pointers? Hah, piece of cake.... Computer architecture? Puh. Smile
Post 23 Aug 2005, 09:41
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
Windows lets you run MZ exe's and COMs. (Not mine though! It just won't work. It worked before but not now anymore. Sad )

_________________
This calls for... Ultra CRUNCHY Man!
Ta da!! *crunch*
Post 23 Aug 2005, 17:26
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
zoijar wrote:

1 - Because it's better to learn from a consistent, coherent whole, than from bits and pieces scrapped from the net.

i disagree here. from one source you are limited by limitations of one single author, you should have multiple sources to overcome such problems. no one, even authors of commercial books, is 100% genius and everyone doesn't explain everything in full (and even if he would no one would read it anyway). if you are stuck, some other source can help you. also learning one-thing-at-time is getting boring after some time, you should work on more things at same time and switch them when one of them gets boring. This way it's more fun and thus you spend more time learning and advance faster.
zoijar wrote:

2 - You're partially correct: the understanding of historical changes is nice. However, none of the modern operating systems really allow you to work in realmode (I'm not sure about virtual 8086 mode). So you would need an old DOS partition or something similar. Besides that most people want to learn something that can be used now. However, learning the historical context and evolution of hardware can be a significant insight; it sure helped me a great deal in university... C pointers? Hah, piece of cake.... Computer architecture? Puh. Smile

sorry, but if somebody wants just to learn something useful then he should stick with PHP, not assembly. (X86) assembly is for people who are interested in how-things-work (eg. hackers Smile ). learning assembly from ring3 port.mode doesn't lead very far and people who want to learn it deeper anyway end in writing own operating system or switching to DOS. So this is unavoidable, and if it is done at the beginning od learning then people save a lot time (it's easier to learn historically forward than backward)
Post 23 Aug 2005, 19:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.