flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > 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: 555
fasmnewbie 15 Jul 2017, 19:08
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 |0000000000401000| 0|0  ;initial memview2 output
61 70 41 20 2E 61 69 6E |0000000000401008| 8|8
3F 72 61 62 61 68 6B 20 |0000000000401010| 10|16
61 6D 69 72 65 54 00 0A |0000000000401018| 18|24 ;byte 25 is 0. 
00 2E 68 69 73 61 4B 20 |0000000000401020| 20|32
00 00 00 00 00 00 00 00 |0000000000401028| 28|40
00 00 00 00 00 00 00 00 |0000000000401030| 30|48
00 00 00 00 00 00 00 00 |0000000000401038| 38|56
00 00 00 00 00 00 00 00 |0000000000401040| 40|64
00 00 00 00 00 00 00 00 |0000000000401048| 48|72

75 44 20 6F 6C 6C 65 48 |0000000000401000| 0|0
61 70 41 20 2E 61 69 6E |0000000000401008| 8|8
3F 72 61 62 61 68 6B 20 |0000000000401010| 10|16
20 61 6D 69 72 65 54 0A |0000000000401018| 18|24
00 00 2E 68 69 73 61 4B |0000000000401020| 20|32
00 00 00 00 00 00 00 00 |0000000000401028| 28|40
00 00 00 00 00 00 00 00 |0000000000401030| 30|48
00 00 00 00 00 00 00 00 |0000000000401038| 38|56
00 00 00 00 00 00 00 00 |0000000000401040| 40|64
00 00 00 00 00 00 00 00 |0000000000401048| 48|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: 555
fasmnewbie 06 Oct 2017, 18:16
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

p/s "bkernel.asm" can be downloaded from "core.zip" on post#1


Last edited by fasmnewbie on 14 Dec 2017, 09:55; edited 18 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



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 06 Oct 2017, 18:30
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: 555
fasmnewbie 13 Oct 2017, 18:28
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: 555
fasmnewbie 13 Oct 2017, 18:31
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: 555
fasmnewbie 16 Oct 2017, 13:29
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



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 16 Oct 2017, 13:50
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: 555
fasmnewbie 16 Oct 2017, 13:57
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: 555
fasmnewbie 16 Oct 2017, 14:20
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: 555
fasmnewbie 23 Oct 2017, 21:36
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: 555
fasmnewbie 23 Oct 2017, 23:34
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

Demo: Binary 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: 555
fasmnewbie 24 Oct 2017, 00:11
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
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 03 Dec 2017, 04:16
I forgot to mention how to access CPULIB from, say C. Here's a little example;

Code:
// DEMO: Importing from CPULIB 64-bit binaries
// gcc -m64 this.c cpu64.o -o this (Linux64)
// or
// gcc -m64 this.c cpu64.dll -o this.exe (Win64)

#include <stdio.h>

#define MAX 100

//Refer to the supplied Documentation (cpudoc)
extern void dumpreg(void);               //both void
extern void dumpxmm(unsigned long long); //one int argument
extern unsigned long long randm(unsigned long long);
extern void str_token(char *,char *);   //two C-string arguments
extern void memview(unsigned long long *,unsigned long long);

int main()
{
        unsigned long long x=0;
        char tok[]="/? .";
        char str[]="/Hello??. / .World... how?are/you";
        unsigned long long *p = &x;
        
        dumpxmm(10); //view output in hex
        
        for (x; x <= MAX ; x++)
                printf("%llu ",randm(MAX));
        putchar('\n');
        
        str_token(str,tok);
        
        putchar('\n');
        memview(p,50);
        dumpreg();
        return 0;
}    

Here's the output from Linux64
Code:
XMM0 : 0|FF00
XMM1 : 2F2F2F2F2F2F2F2F|2F2F2F2F2F2F2F2F
XMM2 : 0|0
XMM3 : 0|FF000000000000
XMM4 : 7465675F6F736476|5F5F00656D697474
XMM5 : 0|0
XMM6 : 0|0
XMM7 : 0|0
XMM8 : 0|0
XMM9 : 0|0
XMM10: 0|0
XMM11: 0|0
XMM12: 0|0
XMM13: 0|0
XMM14: 0|0
XMM15: 0|0
65 35 94 79 63 53 42 39 90 21 28 57 16 22 85 51 30 83 63 18 54 53 100 72 74 22 13 78 16 80 62 46 82 84 42 74 41 52 69 45 60 2 98 89 32 94 49 14 63 36 60 22 10 55 70 54 72 18 55 16 80 71 28 72 41 90 3 11 58 6 60 52 52 30 41 7 59 54 14 87 50 94 54 51 27 5 66 28 53 20 62 58 94 46 33 61 77 61 73 91 56 
Hello
World
how
are
you
0000000000000065 |00007FFF20D82DE0| 0|0
00007FFF20D82DE0 |00007FFF20D82DE8| 8|8
000000002E203F2F |00007FFF20D82DF0| 10|16
0000000000406A1D |00007FFF20D82DF8| 18|24
3F3F6F6C6C65482F |00007FFF20D82E00| 20|32
726F572E202F202E |00007FFF20D82E08| 28|40
            646C |00007FFF20D82E10| 30|48
RAX|00007FFF20D82DE0 RBX|0000000000000000 RCX|00007F94A663A290 
RDX|000000000000000A RSI|0000000000000032 RDI|00007FFF20D82DE0 
R8 |00007F94A6909780 R9 |00007F94A6B3D700 R10|00000000000001D6 
R11|0000000000000246 R12|00000000004004E0 R13|00007FFF20D82F10 
R14|0000000000000000 R15|0000000000000000 RBP|00007FFF20D82E30 
RSP|00007FFF20D82DE0 RIP|00000000004006BC [UHEX]    


Note that not all CPULIB modules are suitable for high-level access. CPULIB attachment is in the first post.
Post 03 Dec 2017, 04:16
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 30 Jan 2018, 03:05
I updated BASELIB to Revision 2.9 due to small bugs popping up here and there in "bkernel.asm" source.
Post 30 Jan 2018, 03:05
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 12 Feb 2018, 10:17
Updated to Revision 3.0. This is a major and milestone update. With LOTS of bug fixes (fatal and non-fatal). Note that the attachment included in the first post is FASM-only attachments to reduce the load.

For other syntaxes and other things, the full load of BASELIB/CPULIB can be downloaded from Sourceforge or my Google+.

Thanks to those who reported the bugs.
Post 12 Feb 2018, 10:17
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 15 Feb 2018, 20:02
A small update and bug fixes to Revision 3.1 that doesn't affect the main source (it was a small bug in "bkernel.asm").

Please note that this is FASM-only attachments. For other syntax (NASM, MASM), you can download the full version of BASELIB/CPULIB from Sourceforge or my Google+ profile.
Post 15 Feb 2018, 20:02
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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.