flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Examples and Tutorials > BASELIB: General purpose libs for beginners

Goto page Previous  1, 2, 3, 4, 5, 6, 7
Author
Thread Post new topic Reply to topic
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
The ability to manipulate the memory directly is essential to X86 programmers. For that reason, I've introduced the "memview2" routine, as a small enhancement to current "memview" routine. This routine will also display the offsets, both in hex and decimal format, in addition to the addresses. This will enable a user to see the content of a memory and go to / manipulate a specific offset directly.

Now that the offsets is clearly visible, I've added "mem_insert" routine to enable one to insert any code or data of any size to a specific offset. This is even more effective if used in combination with "mem_load", like for example, you want to load a DLL. But I'm not going to show that here.

Below is a simple example, featuring both "memview2" and "mem_insert" combo, to extend a 0-ended string, past its 0 delimiter.


Code:
format PE64 console
include 'win64axp.inc'
entry main

section '.data' data readable writeable
msg db 'Hello Dunia. Apa khabar?',0ah,0
insert db 'Terima Kasih.',0

section '.code' code readable executable
main:
        mov     rbx,50h  ;size to view
        mov     rax,msg  ;starting address
        call    memview2 ;view mem + offsets

        mov     rdx,15
        mov     rcx,insert
        mov     rbx,25   ;insert starts at offsets 25
        mov     rax,msg
        call    mem_insert ;append string by overwriting 0 from msg

        call    prnline

        mov     rbx,50h
        mov     rax,msg
        call    memview2

call halt
call exitx



With this output: CONTENT | Address | Hex offset | Dec Offset



Code:
75 44 20 6F 6C 6C 65 48 |00000000004010000|0  ;initial memview2 output
61 70 41 20 2E 61 69 6E |00000000004010088|8
3F 72 61 62 61 68 6B 20 |000000000040101010|16
61 6D 69 72 65 54 00 0A |000000000040101818|24 ;byte 25 is 0. 
00 2E 68 69 73 61 4B 20 |000000000040102020|32
00 00 00 00 00 00 00 00 |000000000040102828|40
00 00 00 00 00 00 00 00 |000000000040103030|48
00 00 00 00 00 00 00 00 |000000000040103838|56
00 00 00 00 00 00 00 00 |000000000040104040|64
00 00 00 00 00 00 00 00 |000000000040104848|72

75 44 20 6F 6C 6C 65 48 |00000000004010000|0
61 70 41 20 2E 61 69 6E |00000000004010088|8
3F 72 61 62 61 68 6B 20 |000000000040101010|16
20 61 6D 69 72 65 54 0A |000000000040101818|24
00 00 2E 68 69 73 61 4B |000000000040102020|32
00 00 00 00 00 00 00 00 |000000000040102828|40
00 00 00 00 00 00 00 00 |000000000040103030|48
00 00 00 00 00 00 00 00 |000000000040103838|56
00 00 00 00 00 00 00 00 |000000000040104040|64
00 00 00 00 00 00 00 00 |000000000040104848|72



Hope this is useful.
Post 15 Jul 2017, 19:08
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I just finished creating a minimal "sbase32w.asm" version of Win32 BASELIB source. Well this one is a very basic version (but working), missing many other functions of the original "sbase32w.asm".

Major difference: This one is developed by using high-level features of FASMW (proc, invoke, stdcall, .if etc).

I don't personally like it due to its high-level nature... but I think ignoring FASM's high-level features completely is not 'productive' either. Some day, you'd be required to come up with Line of Code (LOC) "costing" and knowing some of the high-level features would come handy.

But contrary to popular belief, ASM high-level features are actually NOT for beginners no matter how friendly they look. High features are for those who already appreciated how they work at the low-level layer. If you prefer the low-level, just download BASELIB at the first post.

Good luck with this one. Correct the bugs yourself Very Happy


Description: sbase32w.asm reloaded, with high-level features.
Download
Filename: bkernel.zip
Filesize: 9.44 KB
Downloaded: 25 Time(s)



Last edited by fasmnewbie on 12 Nov 2017, 15:01; edited 16 times in total
Post 06 Oct 2017, 18:16
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6633
Location: Kraków, Poland

fasmnewbie wrote:
But contrary to popular belief, ASM high-level features are actually NOT for beginners no matter how friendly they look. High features are for those who already appreciated how they work at the low-level layer. If you prefer the low-level, just download BASELIB at the first post.

A very wise words. I'd like to emphasize this myself - if you use high level without enough knowledge of what is under the hood, you're going to use it like a whimsical black box that can blow up any minute because you do not really know what it ends up doing in the low level.
Post 06 Oct 2017, 18:30
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Thanks for the support Tomasz. I can see similar trend from the likes of MASM when they dropped some of the high-level features which were once crucial in 32-bit ML. Like "invoke" and .IF/.ENDIF. Microsoft decided to back to the low-level approach.
Post 13 Oct 2017, 18:28
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Just added a new Win64 source file (core64.asm). This source is an equivalent of BASELIB's "base64w.asm", targetted for Win64. The source is in FASM syntax only. File is added in "core.zip" on Page 1.

The differences from "base64w.asm":

1. Partially ABI compliance.
2. Rely only on kernel32 as the main external.
3. Most of the routines now are callable from high-level languages, except those requiring XMM returns and arguments. (This is a future project, if I have time)
4. Routine changes:

Added
----------
mem_alloc2
mem_free2
dble2str
file_delete

Discarded
----------
memview2
memviewb
prnbinf
prndblr
prnfltr

Warning: Still quite ugly and could be buggy. "base64w.asm" and "core64.asm" although similar in functions, are not compatible with each other.

Linux64 version is on the horizon. Can't promise that soon, anyway Wink
Post 13 Oct 2017, 18:31
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Updated the "bkernel.asm" above. Just finished with "fpu_stack".

Bug report: I think FASM's proc32 or stdcall scratches EDX when called/used from within a function and addressing local data via "ADDR" operator. Using the library provided above, here's how the issue emerges (observe dumpreg output of EDX);


Code:
format PE console
include 'win32axp.inc'
entry main

section '.data' data readable writeable
h db 'hello world',0ah,0

section '.text' code readable executable
main:
        call    foo

        ;mov     edx,-13
        ;call    dumpreg
        ;stdcall prnstrz,h       ;these, do not
        ;call    dumpreg

        call    halt
        call    exitx

;--------------------------
proc foo
     locals
        h db 'Hello World',0ah,0
     endl
        mov     edx,10h
        call    dumpreg

        stdcall prnstrz,addr h  ;this clobbers edx

        ;lea     edi,[h]
        ;stdcall prnstrz,edi     ;these do not

        call    dumpreg
        ret
endp

Post 16 Oct 2017, 13:29
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6633
Location: Kraków, Poland
See Windows header documentation, section 2.1:

Quote:
If the parameter is preceded by the addr word, it means that this value is an address and this address should be passed to procedure, even if it cannot be done directly - like in the case of local variables, which have addresses relative to EBP/RBP register. In 32-bit case the EDX register is used temporarily to calculate the value of address and pass it to the procedure.

Post 16 Oct 2017, 13:50
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Ok, got it.

Thought it was a bug.
Post 16 Oct 2017, 13:57
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I re-uploaded the "bkernel.asm" above to demonstrate the use of "DUP(x)" inside a LOCALS...ENDL instead of plain "RB" (reserve byte). Already has a lot of RB in there, but no DUP. If you know any other high-level features that can be included, do modify and share. Thanks.
Post 16 Oct 2017, 14:20
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Using bkernel.asm above or any other BASELIB library, here's one simple demo to learn / teach FPU instruction in a more interactive and efficient manner without going back and forth the debugger.


Code:
format PE console
include 'win32axp.inc'
entry main

section '.data' data readable writeable
msg db 'hello world',0ah,0

section '.text' code readable executable
main:
        ;reset FPU precision, or
        finit
        ;stdcall fpu_precision,1

        fldpi               ;load to ST1
        fchs                ;change sign
        fld1                ;load to ST0
        call    fpu_stack   ;see initial FPU stack

        call    prnline     ;visual separator

        fsub    st0,st1     ;learn FSUB instruction
        call    fpu_stack   ;see the result

        call    halt
        call    exitx



The output from "fpu_stack" after executing FPU's FSUB instruction;

Code:
st0|+1.000000000000000000
st1|-3.141592653589793238
st2...
st3...
st4...
st5...
st6...
st7...

st0|+4.141592653589793238
st1|-3.141592653589793238
st2...
st3...
st4...
st5...
st6...
st7...


Just be reminded that "bkernel.asm" is highly experimental - my first attempt to include as many high-level features of FASMW as possible. If in doubt, just use the much stable BASELIB library.
Post 23 Oct 2017, 21:36
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Now that all the basic modules are available, you can extend them by building some nice macros on top of it. Below is a demo of creating 2 macro extensions from "bkernel.asm" or any other BASELIB sources.

Code:
format PE console
include 'win32axp.inc'
entry main

macro strline a,b
{
        stdcall prnstrz,a
        if ~b eq
           repeat b
               call prnline
           end repeat
        end if
}
macro binary [a]
{
      forward
         stdcall prnbinb,a
         strline '='
         strline `a,1
}

section '.data' data readable writeable
msg db '2 new lines',0

section '.text' code readable executable
main:
        strline msg,2
        strline "Demo: Binary Arithmetics & Logical Operations"

        call    prnline
        binary  eax,EBX,ecx      ;use the macro

        or      eax,ebx         ;Practice using OR

        call    prnline
        binary  ebx,EaX

        call    halt
        call    exitx 


The output shows two named registers are placed tight to each other after performing an OR bitwise operation on them using "binary" macros built on top of "prnbinb" routine.

Code:
2 new lines

DemoBinary Arithmetics & Logical Operations
01110101 11000100 11101111 00001010=eax
00000000 00000000 00000000 00110001=EBX
00000000 00000000 00000000 00000000=ecx

00000000 00000000 00000000 00110001=ebx
01110101 11000100 11101111 00111011=EaX

Post 23 Oct 2017, 23:34
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Another more focused example, this time using bitwise AND on EAX and EBX registers

Code:
format PE console
include 'win32axp.inc'
entry main

macro strline a,b
{
        stdcall prnstrz,a
        if ~b eq
           repeat b
               call prnline
           end repeat
        end if
}
macro binary [a]
{
      forward
         stdcall prnbinb,a
         strline '='
         strline `a,1
}

section '.text' code readable executable
main:
        strline "Bitwise AND EAX(345),EBX(102)",2
        mov     eax,345
        mov     ebx,102

        binary  EAX,EBX         ;See initial bits
        and     eax,ebx         ;Practice AND bitwise

        strline "-----------------------------------",1
        binary  EAX             ;see bit changes after AND

        call    halt
        call    exitx


Output

Code:
Bitwise AND EAX(345),EBX(102)

00000000 00000000 00000001 01011001=EAX
00000000 00000000 00000000 00110001=EBX
-----------------------------------
00000000 00000000 00000000 00010001=EAX



Hope you like it Very Happy
Post 24 Oct 2017, 00:11
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4, 5, 6, 7

< 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.