Assembler in Freebasic

New to FreeBASIC? Post your questions here.
Post Reply
SoCalTechnologies
Posts: 3
Joined: Oct 13, 2017 1:55

Assembler in Freebasic

Post by SoCalTechnologies »

Does Freebasic support the assembler tutorials on Pete's Qbasic page?
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Assembler in Freebasic

Post by St_W »

You didn't tell us which tutorials exactly, but in general using Assembler with FreeBasic is a bit different than using it in QB:
- FreeBasic supports Inline Assembler while QB didn't
- FreeBasic is 32-bit or 64-bit while QB was 16-bit; so you'll have to use according assembly code
- FreeBasic runs in protected mode, while QB ran in real mode
- QB ran on DOS only; FB runs on Windows, Linux, DOS and others, so you can't rely on DOS APIs if you want a portable application; anyway you'd have to use DPMI even in DOS to access DOS APIs (due to the protected mode / real mode transition)
SoCalTechnologies
Posts: 3
Joined: Oct 13, 2017 1:55

Re: Assembler in Freebasic

Post by SoCalTechnologies »

http://www.petesqbsite.com/sections/tut ... mbly.shtml

"Learn ASM In One Hour Flat" Series by abionnnn

It is down near the bottom of list.

Where are the Freebasic Assembler tutorials?
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Assembler in Freebasic

Post by Trinity »

There is a page in the manual about ASM : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgAsm

You can also find an "Inlineassembler in FreeBASIC" tutorial here , but text is in German (though I doubt if the code cares ;-) :
https://www.freebasic-portal.de/tutoria ... ic-50.html

Also there is an Inline assembler discussion thread here :
viewtopic.php?t=25549

Then there is this , but I guess that you want something more advanced : Lil' ASM tutes: viewtopic.php?t=21574
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Assembler in Freebasic

Post by MrSwiss »

SoCalTechnologies wrote:Where are the Freebasic Assembler tutorials?
Didn't ever look for those (anyway, not the core busyness, of FB, to teach ASM) ...
FB's inline assembler is for programmers, that are used to code ASM (not beginners).

However, plenty examples can be found ... (searching this forum).
SoCalTechnologies
Posts: 3
Joined: Oct 13, 2017 1:55

Re: Assembler in Freebasic

Post by SoCalTechnologies »

Thank you.

Will keep you posted.

Looks like it is 'Learn Assembler in One Hour' for now.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Assembler in Freebasic

Post by grindstone »

"Learn ASM In One Hour Flat"
That's real optimism. <grin>
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Assembler in Freebasic

Post by BasicCoder2 »

SoCalTechnologies wrote:Looks like it is 'Learn Assembler in One Hour' for now.
If you are using a 32 bit machine the tutorial is perhaps a bit out of date. Also by using the FreeBASIC inline assembler code you can mix both worlds as opposed to learning assembler using a compiler like the old TASM. I used to use 16 bit assembler as my language of choice on the old MSDOS machines and before that 32 bit assembler on the old 68000 cpu Amiga machine. I often worked out the code in BASIC and then translated down to assembler code. For example this turns a 640x480 bitmap color image into a gray scale version. It is written only in FreeBASIC but does the same thing as the example that follows that is written using inline assembler. It may give you an idea of how you can use your knowledge of BASIC to leverage into Assembler code.
You must have a 640x480 bitmap image in the same folder and change the source code below with your own bitmap file name.
I used a bitmap image called boat2.bmp
Bload "boat2.bmp", image 'place 640 x 480 image in same folder

Code: Select all

#include "fbgfx.bi"
screenres 640,480,32

Dim image As Any Ptr
image = imagecreate( 640, 480 )

Bload "boat2.bmp", image   'place 640 x 480 image in same folder
Put(0, 0), image
sleep

dim as ulong eax,edx   'variables with same name as the CPU 32 bit registers

for j as integer = 0 to 479
    for i as integer = 0 to 639
        eax = point(i,j,image) 'mov eax, [ebx+ecx*4]
        eax = eax AND 255      'And eax, 255
        edx = point(i,j,image) 'mov edx, [ebx,ecx*4]
        edx = edx SHR 8        'shr edx, 8
        edx = edx AND 255      'and edx, 255
        eax = eax + edx        '
        eax = eax SHR 1
        edx = point(i,j,image)
        edx = edx SHR 16
        edx = edx AND 255
        eax = eax + edx
        eax = eax SHR 1
        edx = eax
        eax = eax SHL 8
        eax = eax OR edx
        eax = eax SHL 8
        eax = eax OR edx
        pset image,(i,j),eax
    next i
next j

Put(0, 0), image, Pset

imagedestroy(image)

Sleep
Now how to do it with inline assembler,

Code: Select all

#include "fbgfx.bi"
screenres 640,480,32

Dim buff As Any Ptr
buff = imagecreate( 640, 480 )

Bload "boat2.bmp", buff   'place 640 x 480 image in same folder
Put(0, 0), buff

'' Skip over image header.
Dim As Any Ptr ppix = buff + sizeof( fb.image )

Sleep

asm
    mov ecx, 640*480      '
    mov ebx, [ppix]       '
  L1:
    mov eax, [ebx+ecx*4]  '
    And eax, 255          ' gets blue
    mov edx, [ebx+ecx*4]  '
    Shr edx, 8            '
    And edx, 255          ' 
    add eax, edx          ' add green
    Shr eax, 1
    mov edx, [ebx+ecx*4]
    Shr edx, 16
    And edx, 255
    add eax, edx          ' add red
    Shr eax, 1
    mov edx, eax
    Shl eax, 8
    Or  eax, edx
    Shl eax, 8
    Or  eax, edx
    mov [ebx+ecx*4], eax
    dec  ecx
    jnz  L1
End asm

Put(0, 0), buff, Pset

imagedestroy( buff )

Sleep
Last edited by BasicCoder2 on Oct 16, 2017 8:03, edited 1 time in total.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Assembler in Freebasic

Post by jj2007 »

SoCalTechnologies wrote:"Learn ASM In One Hour Flat" Series by abionnnn
© Copyright Nightwolf Productions 1999

That is 16-bit assembly, won't work on any modern PC. For more recent x86 tutorials, see e.g. Help and tutorials, last four entries starting with Iczelion.
Post Reply