flat assembler
Message board for the users of flat assembler.

Index > DOS > My first FASM!!!!

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 16:14
Well I've finally finished my very first FASM proggy! In fact, it's my first ever ASM program! Shocked

It's a very simple Dec->Bin Byte converter. Compiles down to 47 Bytes! Whooo! I couldn't do THAT in VB6! Laughing

Code:
org 100h        ; start address (origin) of program.
                ; init vars and registers.
mov cl,128      ; init cl  - 128 is the highest bit value in our byte. This is out comparator.
mov ah,2        ; we use 2 (with int 21h) to output one character (byte) at a time.
start:
;move original to temp register
mov bl, [Val2Cnv]
and bl,cl       ;AND our temp value with comparator (CL)
cmp bl,cl       ;Compare result of AND
je o1           ;Set output as 0
mov dx, 30h
jmp o           ;jump to the actual output
o1:             ;Set output as 1
mov dx, 31h
o:
int 21h         ;output the result we set
shr cl, 1       ;bit shift (effective divide by 2!)
cmp cl, 0       ;check if we're at zero (ie, all done)
ja start
ThatsAllFolks:
;finish up
mov dx,13       ;CR character
int 21h         ;execute the output (ah is 2)
mov dx,10       ;LF character
int 21h         ;execute the output (ah is 2)
mov ax,4c00h    ;exit nicely and tell the OS all is well!
int 21h         ;execute the exit

Val2Cnv db 125  ; the value to convert.
    


There's still a long way to go though:
I need to optimize this kiddie to use TEST instead of add/cmp.
I need to try to directly output the result of the Zero Flag. This would get rid of the jumps!
The value is hardwired. Very bad! Must work out how to get a parameter in from the CLI.
Lastly, it should be turned into a proc/macro/whatever+parameter to allow easy calling. I'm a total ASM noob, so some more research is necessary!

Any comments/advice would be appreciated! Wink

_________________
Should've ducked faster!
Post 03 Nov 2006, 16:14
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 03 Nov 2006, 17:21
The thing that stands out as very good for a ASM noob, is you comments, this may of come from you coding in other lanuages, well done.

PS: The "126-byte .ZIP viewer" get input from CLI.
Post 03 Nov 2006, 17:21
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 03 Nov 2006, 17:32
int 21h,2 only uses DL, so you can save a byte each time: Smile

Code:
BA1300                   MOV DX,13
B213                     MOV DL,13
    


Oh, and here's a (quick) example on how to use macros:

Code:
macro putc char {

mov ah,2
mov dl,char
int 21h

}

org 100h

putc 'H'
putc 'i'
putc '!'

int 20h
    


In a DOS .COM, the PSP:80h is where the cmdline starts. byte [80h] is the length of the cmdline (excluding the carriage return, aka 0Dh).

For example (C.COM is the 126-byte .ZIP viewer):

Quote:

c fasm167.zip


The length of the cmdline at byte [80h] would be 12 (aka, 0Ch).

(Bah, I'll just keep editing/adding to this one post.)

See Ralf Brown's interrupt list (online or download it) for more DOS goodness! Very Happy


Last edited by rugxulo on 03 Nov 2006, 17:46; edited 3 times in total
Post 03 Nov 2006, 17:32
View user's profile Send private message Visit poster's website Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 17:36
Thanks for that Dex! Well, I went mad with comments because:

A. I still remember the pain of writing a tight BBC Model B loop and then coming back to it six months later thinking Shocked WTF does that DO??!!! Wink
B. I want to use my experiences with FASM to put together some tutorials so that others can get the benefits of my research. (And believe me, just putting THAT little thing together took days of reading!)
C. Like I said, I'm an ASM noob, so let's try to start out with good habits and keep them! Wink
Post 03 Nov 2006, 17:36
View user's profile Send private message Visit poster's website Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 17:38
Quote:

int 21h,2 only uses DL

Thanks, I'm sure I read DX somewhere, but seeing as only outputs a byte that makes a lot of sense! Thanks! Very Happy

Oh, and BTW, coming from a Basically Visual world *cough*, I'd organise a program like:

MainLoop
Call Sub1
Call Sub2
Call sub3
If bExit then Exit

Sub1

Sub2

Sub3

Do I presume that the macro(s) MUST be defined in the "header" (for want of a better word) before the "main" code in FASM?
Post 03 Nov 2006, 17:38
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 03 Nov 2006, 17:51
Apparently, in my just-now-conducted test, it does need to be defined before used. Shouldn't be that hard to keep track of, IMO. You may even want to put all your macros in an include file to keep them separate from the main code. Oh, and don't forget, you can overload instructions (MOV, XCHG, etc.) with macros in FASM. Very Happy

FASM documentation online:
Post 03 Nov 2006, 17:51
View user's profile Send private message Visit poster's website Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 18:48
rugxulo,

Thanks for this, I had read the docs before, but there's so much to take in and understand that I never remember where I actually read it! Your post is a great help! Very Happy

Oh and PS, Yeah, I grabbed Ralf's list. It's massive! However, even with the included veiwers I found that:

A. It's almost TOO inclusive... Int this with ax that = returns 1234 if SOandSO Virus is loaded.
B. No examples.
C. It's not very noob-friendly. Mind you, how could it be?

I hope that does't come across as a critism. Just putting that lot together in the first place is a massive achievement!! It's just that I like to learn by "getting my hands dirty" rather than reading loads of dry text. I actually kicked off best with Chandler's tutorial, his enthusiasm for asm (well, binary, really) really rubs off and get's you in the mood to "play" and explore the possibilities!

I'm sure that as I learn and gain experience, It'll become an invaluable resource! Very Happy[/i]

_________________
Should've ducked faster!
Post 03 Nov 2006, 18:48
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 03 Nov 2006, 19:58
Nice coding for your first ASM program, VERY nice indeed. I concur with Dex. Also, you make some good points about the over whelming nature of Ralph's list, however, with some experience you'll see it differently and understand that if there where examples for eveything it would even be that much bigger. Give a look see at Randal Hyde's website for some pointers, etcetera.
Post 03 Nov 2006, 19:58
View user's profile Send private message Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 20:37
Thanks, smiddy, I'll take a look once I've macro-ed up this baby! Wink

I just found out something interesting and not in the docs (well, it's bloody obvious TBH, but I hadn't taken account for it - DOH!) I started using the "Byte" converter (I also wrote a word converter) and it failed miserably.

OI!!! Stop bleedin' laughing you lot!!!

The arg to a macro gets passed over as a word, not a byte, so you can imagine the hilarility when I realised how stupid I am!

Actually, as a word address... jeez! I LOVE FASM!!!!!!!!! All that reading AoA, etc... and understanding NOTHING was not in vain! It's all starting to fall into place! Don't you love those "AHA!!!!! moments" Wink

FFS I'm just totally in awe of the power this! It's like being "a kid in a candy store" ... Only as a grown up.... with a big fat wallet!

If the FASM devel team where girls I'd flippin' kiss 'em all! Very Happy

And the community is so friendly too, none of that "well, if don't understand hard-drive addressing vectors then **** off" rubbish!

I think I'm falling in love again. The last time I felt like this was when I saw Defender running on a BBC Model B!!! I mean, it's a Friday night, I'd rather rather be hacking away at ASM than going to the pub and chasing women.






... mind you, THAT could be because of that endless-loop-error "**** off, ugly!!" Laughing
Post 03 Nov 2006, 20:37
View user's profile Send private message Visit poster's website Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 03 Nov 2006, 21:19
Well, I'm obviously missing something.

My macro'd code looks right, compiles fine, runs without errors but produces incorrect results. Since I've been hacking away at this for around 13 hours, I'm calling it quits. I'm obviously missing something stupid and I'll RTFM in the tomorrow!
Post 03 Nov 2006, 21:19
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 03 Nov 2006, 22:14
some times it is a good idea to take a break and get a fresh start after a few winks.
Post 03 Nov 2006, 22:14
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 03 Nov 2006, 22:56
Hey eVAPor8 you're that guy who emailed me! Been reading my tutorials I see. People who read what I write seem to develop an obsession with binary. Well,glad you did it. We need more people like you.
Post 03 Nov 2006, 22:56
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 03 Nov 2006, 23:11
BTW,here's my program that counts every byte value.

Code:
org 256

xor ecx,ecx

p0:

mov bx,8
mov eax,ecx
inc ecx

p1:
dec bx
mov edx,eax
and dl,1
shr eax,1
or dl,48
mov [b+bx],dl
cmp bx,0
ja p1

mov ah,9
mov dx,b
int 33

cmp ecx,255
jbe p0

int 32
b db 8 dup 48,13,10,36
    
Post 03 Nov 2006, 23:11
View user's profile Send private message Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 04 Nov 2006, 09:46
AAAA---HA!!!!!

So now I know who 2 is ! Laughing Cool

As it turns out, I've also read a lot of your posts. (I did a lot of lurking before I registered) You've advanced far further in the world of asm than me, but I'll run that code today (If I get time) and try work out what's going on. I don't understand the purpose of that first XOR on ecx!

God I HATE being a noob. I'm so used to being the guy that eveyone calls on for help it's embarrasing!

.... but discovering this new world is so much FUN!!!

I'll have a look at my code this PM (UK) and see if I can correct it, if not, I'll post it up and ask for help! I'm obviously missing something fundamental and critical!

All the best, everyone, and thanks for the help!

eVAP

_________________
Should've ducked faster!
Post 04 Nov 2006, 09:46
View user's profile Send private message Visit poster's website Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 04 Nov 2006, 13:19
eVAPor8 wrote:
I don't understand the purpose of that first XOR on ecx!


xor ecx, ecx = zero ecx

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 04 Nov 2006, 13:19
View user's profile Send private message MSN Messenger Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 04 Nov 2006, 18:09
okasvi wrote:
eVAPor8 wrote:
I don't understand the purpose of that first XOR on ecx!


xor ecx, ecx = zero ecx

::NOOB ALERT:: Wink
That's what I thought, but why not

mov ecx, 0h

Seems simpler, quicker and more "readable" to me. I'm obviously missing something! Wink I can't imagine that xor use less cycles than mov, so what's the point? What am I missing?

_________________
Should've ducked faster!
Post 04 Nov 2006, 18:09
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 04 Nov 2006, 18:31
xor ecx,ecx is assembled to 31 C9
mov ecx,0 is assembled to B9 00 00 00 00
They are both one-cycle once decoded, but it is better to use short instructions so the processor does not use as much time reading code from memory.
Post 04 Nov 2006, 18:31
View user's profile Send private message Reply with quote
eVAPor8



Joined: 03 Nov 2006
Posts: 12
Location: UK
eVAPor8 04 Nov 2006, 20:02
Goplat wrote:
xor ecx,ecx is assembled to 31 C9
mov ecx,0 is assembled to B9 00 00 00 00
They are both one-cycle once decoded, but it is better to use short instructions so the processor does not use as much time reading code from memory.

Thanks for this! I have SOOOO much to learn! Embarassed

It's funny, it's both scary as hell, but exciting too! Very Happy

_________________
Should've ducked faster!
Post 04 Nov 2006, 20:02
View user's profile Send private message Visit poster's website Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 04 Nov 2006, 21:34
The other advantage is the program size will be fewer bytes!
That's the big reason I like XOR. Also,notice I did 'or dl,48' instead of
'add dl 48' ? Well,I like to use bitwise operations when possible. I read that
they are faster.
Post 04 Nov 2006, 21:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 04 Nov 2006, 21:51
2, are you replacing every ADD with OR? note that it's not the same, it produce the same result only when the 1s bits have a 0 bit in the other operand, otherwise it produces wrong result.

Code:
    1010    1010
ADD      OR
    1010    1010
   =====   =====
   10100   01010    


Of course it's right in your case because 48 is 110000 and the numbers between 0 and 9 occupy only the four less significant bits.
Post 04 Nov 2006, 21:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.