Simple Assembler

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Simple Assembler

Post by Prime Productions »

The following is the idea on what goes on behind an assembler. I thought it might be useful to somebody who is interested in knowing how assemblers work.

It translates assembly code to machine code, and outputs a DOS .COM file. The program it outputs initializes screenres 320x200x8 and exits.

Other than that it is useless. (My specialty)

Code: Select all

'An Ultra-Simple Assembler example, to demonstrate how to code an assembler. This
'assembler takes assembly code, and then outputs a simple DOS .COM file. It 
'currently dosn't support many commands, but it is meant just as an example. If you
'change any of the assembly source, it won't compile properly, without editing the
'output loop.
'
'This asm example initializes screenres 320x200x8
'
'.This is probably worthless.
'
'-David Gutierrez 

Dim OP_RET As Integer = &HC3 'The position is HEX for the opcode ret
Dim OP_MOVAH As Integer = &HB4 'The position is HEX for the opcode MOV AH
                               'MOV is simply &HB, the 4 denotes the register AH
Dim OP_MOVAL As Integer = &HB0 'Same as above, except the 0 is for the register AL
Dim OP_INT As Integer = &HCD 'Hex position for the RET

Dim Code(3) As String 'Out assembly code array
Code(1) = "mov ah, 0" 'Mov 0 into the AH register.
Code(2) = "mov al, 13" 'Mov 13 into AL. This is so as to initialize screen 320x200x8
Code(3) = "int 10" 'Call interupt 10, for the video.

Dim hex10 As Integer = &H10 '10 in hex
Dim hex13 As Integer = &H13 '13 in hex

Dim F As Integer
F = FreeFile 'Get next availiable file handle

Dim Org As Integer = &H100 'The position to load the code for a DOS .COM application.

Open "asm.com" For Binary As #F 'Open for binary

'Pad the code so as to start at 100h for a DOS .COM file.
Dim myint As Integer = &HEB
Dim i As Integer
Dim b As Integer = &H00
For i = 1 To &H100 Step 2
	Put #F, i, myint
	Put #F, i + 1, b
Next

'Output our ASM code.
For i = 1 To 3 'loop for a three code lines.
	Org = Org + 2
	Select Case Trim(Left(UCase(Code(i)), 3), " ")
		Case "MOV"
			If Trim(Mid(UCase(Code(i)), 5, 2), " ") = "AH" Then 'the register
				Put #F, Org-1, OP_MOVAH
				Put #F, Org, Val(Mid(UCase(Code(i)), 8, Len(Code(i))-7))
			ElseIf Trim(Mid(UCase(Code(i)), 5, 2), " ") = "AL" Then 'the register
				Put #F, Org-1, OP_MOVAL
				If Val(Mid(UCase(Code(i)), 8, Len(Code(i))-7)) = 13 Then
					Put #F, Org, hex13
				EndIf 
			EndIf  
		Case "INT"
			Put #F, Org-1, OP_INT
			If Val(Mid(UCase(Code(i)), 4, Len(Code(i))-3)) = 10 Then
				Put #F, Org, hex10
			End If
	End Select

Next

Put #F, org+1, OP_RET 'Return


Close #F 'Close our file
Enjoy!
David
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

nice try
to bad that the DOS days are over ;-)

here are how you can create

very tiny ELF (Linux executable)
http://www.muppetlabs.com/~breadbox/sof ... eensy.html

very tiny PE (Windows executable)
http://www.phreedom.org/solar/code/tinype/

Joshy
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Post by Prime Productions »

D.J.Peters wrote:nice try...
Thanks, my first go at outputting binary directly. Thanks for the links. The ELF Linux format looks simple. The windows one looks rather messy though.

David
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Prime Productions wrote:The Linux format looks simple. The windows one looks rather messy though.
=P

Sorry, just being goofy ;)
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Post by Prime Productions »

cha0s wrote:
Prime Productions wrote:The Linux format looks simple. The windows one looks rather messy though.
=P

Sorry, just being goofy ;)
I guess you're on Windows??? So am I actually, (mainly) but I sometimes use Linux Mint.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Prime Productions wrote:
cha0s wrote:
Prime Productions wrote:The Linux format looks simple. The windows one looks rather messy though.
=P

Sorry, just being goofy ;)
I guess you're on Windows??? So am I actually, (mainly) but I sometimes use Linux Mint.
Nah man... Linux all the way actually. I was running Arch but the *buntu's are really getting quite good (fast and user-friendly), so I'm back on that again.
JaDogg
Posts: 345
Joined: Apr 13, 2008 12:11
Location: Sri Lanka - Negombo
Contact:

Post by JaDogg »

you can use ollydbg(part ??) dll to asemble strings
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

DOS and ASM 8088 is gone? no way..

Post by kiyotewolf »

I don't think personally that this is a waste, you can use inline assembly in FB, so this gives people an idea of how you even begin to think in that headspace.

Plus, I still use DOS semi-religiously. I even have a computer re-formatted back to DOS, with Win 3.11, an actual 8-bit SoundBlaster with drivers (lucky score with google on that one), and such.

Even better, I ALMOST scored an original CGA card.



~Kiyote!

This idea borders on being re-itterated in the DOS forum subsection, since is has more meaning there.
nitrofurano
Posts: 57
Joined: Dec 08, 2010 14:55
Location: Portugal
Contact:

Re: DOS and ASM 8088 is gone? no way..

Post by nitrofurano »

kiyotewolf wrote:I don't think personally that this is a waste, you can use inline assembly in FB, so this gives people an idea of how you even begin to think in that headspace.

Plus, I still use DOS semi-religiously. I even have a computer re-formatted back to DOS, with Win 3.11, an actual 8-bit SoundBlaster with drivers (lucky score with google on that one), and such.

Even better, I ALMOST scored an original CGA card.

~Kiyote!

This idea borders on being re-itterated in the DOS forum subsection, since is has more meaning there.

not only it is not a waste, that there are hardware still being sold optionally with FreeDOS as OEM (at least from Dell, i think) - as FreeDOS is almost one of the very few ways to recycle old hardware, and people still code on them - just imagine such a project of making indie arcade machines with this kind of hardware (like Torontron, Winnitron, etc., but with older hardware instead), retro development contests related to it, etc.! :)
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Re: Simple Assembler

Post by kiyotewolf »

You can root Windows 98 down to DOS with a few switches in one of the *.SYS files.

I've got the notes handy, I just had them.

Yeah, you're right, my brother's boss, who runs a pizza place, builds computers into arcade console cabinets, and instead of using arcade monitors, he routinely upgrades to flat screen monitors, and works hard to keep the illusion of it still being a tube TV CRT, with cleverly placed cardboard "shutters" around the edges of the screen.

And, he runs MAME on these Windows 98 [with no gui] DOS, machines, and they run great.

If you think about it, DOS is a bunch of either scripting language, or it's the list of commands, you routinely regularly use, and what does Linux have to offer, in it's repotaur?

sudo du

And a whole bunch of other things, you type in to a text based console window.

As long as people use 16 bit binaries, they will need functionality to run these programs, a DOS environment, shell, or API which provides a wrapper for missing functionality, like how Wine is a bandaid on running Windows programs on Linux? Why not just install Windows as a dual boot? "Cause Windows is a virus." - which is a really, interesting retort.



~Paul!

DOS won't die, it's been here for far too long. Not since jDOSBOX has ensured it will live forever.

Also as long as Harvard architecture ... oh never mind I'm fighting a losing battle.

And now for a quote.
Although not binary compatible, it was designed to allow assembly language programs written for these processors (as well as the contemporary 8085) to be mechanically translated into equivalent 8086 assembly.
If they would auto - translate the 8086 - - - - -> 80586 code into native ARM RISC instructions, there would be no issues, whatsoever. Period. They need a x86 / 8086 co - processor, on these ARM CPU chips, so they don't exclude a whole entire generation of computer users and their still valid software.

We always have to re - invent the wheel. Why can't we just keep using the wheel, and re - invent the road.
akumadantai
Posts: 34
Joined: Apr 03, 2012 13:09

Re: Simple Assembler

Post by akumadantai »

i once made 2 assemblers using freebasic.
one i called : opsulator, the second remains unfinsihed out of disintrest.

i did this in the most simplest and at the same time weirdest ways that could be optimized beyond belief.
but it worked and did its job. i was capapble of recreating an old dos game in it from which i had the source.

but the weirdest part was that i did not have an opcode list to base it on. so it took a mighty long time dissasembling things to then add the opcodes to the list and the bytes as hex valeus.

i wouldnt even try to make one again, as my knowledge about assembler is currently returning to an altime low. the ultimate basic knowledge that is pretty useless by now.
Post Reply