Author Topic: BASELIB: General Purpose Lib for Beginners  (Read 6551 times)

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #30 on: March 27, 2017, 02:13:48 PM »
BASELIB Revision 1.1.16

core.zip (source)
binary.zip (binaries)

What's New (May 8, 2017)

[1] Fixed stack corruptions in "prndbl" and "dbl2str" in all SBASE3 variants.


You can visit my profile for similar downloads and other things here

or you can directly download them from my drive
« Last Edit: May 08, 2017, 10:04:23 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #31 on: June 02, 2017, 05:18:52 PM »
BASELIB Revision 2.0

core.zip (source)
binary.zip (binaries)

What's New (July 16, 2017) Revision 2.0
[1] Added "memview2"
[2] Added "mem_insert"
[3] Added "byte_count"

For latest downloads:

You can visit my profile for similar downloads and other things here

or you can directly download them from my drive

You can safely ignore other attachments found previously in this thread.
« Last Edit: July 27, 2017, 10:30:51 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #32 on: August 19, 2017, 04:38:43 PM »
BASELIB Revision 2.4 (September 28th, 2017)

Attached: core.zip (source), binary.zip (binaries), corelib.zip(source and binaries).

What's New
[1] Fixed a fatal bug in prnflt, prnfltr, flt2str that causes dumpmmx, dumpxmm and dumpymm to freeze on divide-by-zero exception operating on REAL4 floats data. All sources and binaries are affected by this fix.

For latest downloads:

- You can visit my profile for similar downloads and other things here

- or you can directly download them from my drive

or from sourceforge

Please ignore other attachments of similar libraries found in the previous posts.
« Last Edit: October 15, 2017, 11:18:45 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #33 on: October 02, 2017, 02:07:58 AM »
If you have AVX CPU and want to start learning AVX programming, then BASELIB offers various utilites to do just that via prnymm, dumpymm, clearymm and sse_flags routines. They are accessible in both binaries mode and source mode supplied in the attachment (Revision 2.4) in the above post.

Sample demo code (via binaries BASE3.DLL on 32-bit Windows with AVX support, using GCC as the linker)
Code: [Select]
;--------------------
;nasm -f win this.asm
;gcc -m32 this.obj base3.dll -s -o this.exe
;--------------------
global _WinMain@16

extern _clearymm
extern _dumpymm
extern _prnline

section .data align=32
w dq 10.23,11.43,12.54,17.95
x dq 89.54,98.14,55.16,19.11
y dq 45.12,65.22,14.17,29.16
z dq 56.33,51.01,54.86,28.54
a dq 00.00,05.00,00.00,00.00

section .text
_WinMain@16:
        push    ebp             ;C's standard prologue
        mov     ebp,esp
        call    _clearymm       ;YMMs are dirty
        vmovapd ymm4,[w]        ;data population
        vmovapd ymm5,[x]
        vmovapd ymm6,[y]
        vmovapd ymm7,[z]
        vmovapd ymm0,[a]
        mov     eax,1           ;option to view as packed quads
        call    _dumpymm        ;See initial population
        call    _prnline
        vdivpd  ymm1,ymm4,ymm0  ;perform an AVX packed division. YMM1 = YMM4 / YMM0
        mov     eax,1           ;See the result after division
        call    _dumpymm
        pop     ebp             ;Epilogue
        ret

The code above produces the following output
Code: [Select]
YMM0: 0.0|0.0|5.0|0.0   ;the YMM registers after data population
YMM1: 0.0|0.0|0.0|0.0
YMM2: 0.0|0.0|0.0|0.0
YMM3: 0.0|0.0|0.0|0.0
YMM4: 17.95|12.54|11.43|10.23
YMM5: 19.11|55.16|98.14|89.54
YMM6: 29.16|14.17|65.22|45.12
YMM7: 28.54|54.86|51.01|56.33

YMM0: 0.0|0.0|5.0|0.0   ;the YMM registers after packed divisions on four REAL8 data
YMM1: #|#|2.286|#
YMM2: 0.0|0.0|0.0|0.0
YMM3: 0.0|0.0|0.0|0.0
YMM4: 17.95|12.54|11.43|10.23
YMM5: 19.11|55.16|98.14|89.54
YMM6: 29.16|14.17|65.22|45.12
YMM7: 28.54|54.86|51.01|56.33

EDIT: Added the equivalent code for Linux32. Using BASE3.O and GCC as linker. Your CPU must have AVX support.
Code: [Select]
;--------------------
; nasm -f elf this.asm
; gcc -m32 this.o base3.o -s -o this
; ./this
;--------------------
global main

extern clearymm
extern dumpymm
extern prnline

section .data align=32
w dq 10.23,11.43,12.54,17.95
x dq 89.54,98.14,55.16,19.11
y dq 45.12,65.22,14.17,29.16
z dq 56.33,51.01,54.86,28.54
a dq 00.00,05.00,00.00,00.00

section .text
main:
push ebp ;C's standard prologue
mov ebp,esp
call clearymm ;YMMs are dirty
vmovapd ymm4,[w] ;data population
vmovapd ymm5,[x]
vmovapd ymm6,[y]
vmovapd ymm7,[z]
vmovapd ymm0,[a]
mov eax,1 ;option to view as packed quads
call dumpymm ;See initial population
call prnline
vdivpd ymm1,ymm4,ymm0 ;perform an AVX packed division
mov eax,1 ;See the result after division
call dumpymm
pop ebp ;Epilogue
ret
« Last Edit: October 02, 2017, 03:31:54 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #34 on: October 05, 2017, 02:55:28 AM »
This example below demonstrates how one safely probe the system's kernel on Linux for any valuable information returned. Example, in accessing MMAP and MUNMAP, with dumpreg as the helper to verify values returned in RAX. dumpreg routine is accesible from base6.o

Code: [Select]
;-------------------------
;nasm -f elf64 this.asm
;ld this.o base6.o -o this (base6.o is from BASELIB)
;./this
;-------------------------
global _start

extern dumpreg ;from base6.o

section .text
_start:
mov r9,0 ;unsigned long off
mov r8,-1 ;unsigned long fd
mov r10,0x21 ;unsigned long flags
mov rdx,0x3 ;unsigned long prot
mov rsi,0x1000 ;unsigned long size
mov rdi,0 ;unsigned long addr
mov rax,9 ;mmap()
syscall
call dumpreg ;confirm return value in RAX (This is your new allocated area)

dealloc:
mov rsi,0x1000
mov rdi,rax
mov rax,11 ;munmap()
syscall
call dumpreg ;success RAX = 0

exit: xor edi,edi
mov eax,60
syscall

;Equates (from <sys/mmap.h>)
;MAP_SHARED equ 0x1
;MAP_ANONYMOUS  equ 0x20
;PROT_READ  equ 0x1
;PROT_WRITE equ 0x2

Once you are satisfied with all the information returned, then you can delete / disable the calls to base6.o (dumpreg). Output from the 2 dumpregs used above:

Code: [Select]
RAX|00007FE9C14D5000 RBX|0000000000000000 RCX|00000000004000D9  ;RAX points to new area allocated by MMAP.
RDX|0000000000000003 RSI|0000000000001000 RDI|0000000000000000
R8 |FFFFFFFFFFFFFFFF R9 |0000000000000000 R10|0000000000000021
R11|0000000000000202 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|00007FFFF4343390 RIP|00000000004000D9 [UHex]

RAX|0000000000000000 RBX|0000000000000000 RCX|00000000004000ED  ;RAX indicates MUNMAP is successful. Memory allocated is returned.
RDX|0000000000000003 RSI|0000000000001000 RDI|00007FE9C14D5000
R8 |FFFFFFFFFFFFFFFF R9 |0000000000000000 R10|0000000000000021
R11|0000000000000206 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|00007FFFF4343390 RIP|00000000004000ED [UHex]

You can further verify the newly allocated area using other advanced routines from BASELIB such as memview and memview2 by importing them via externs.

You can download BASELIB from the post above (revision 2.4) or from sourceforge

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #35 on: October 05, 2017, 03:11:04 AM »
REVISION 2.7
Nov 19th, 2017

[1]Minor fixes to "sort_byte". Now sort signed bytes only
[2]Minor fixes to "sort_int" stack versions - One volatile register not saved
[3]"memview2" is switched with "memview" (64-bit)
[4]"memview2" is dropped - replaced by "memview" (32-bit)
[5]Minor fixes to mem_alloc/mem_free(Win64)

Introducing "cpulib.zip" - A partially ABI-compliant version of BASELIB. Callable from C. This is not compatible with BASELIB. For Win and Linux.

Download BASELIB. Or download directly below (revision 2.7)
« Last Edit: November 22, 2017, 04:06:38 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #36 on: December 25, 2017, 12:45:06 PM »
(23.Feb.2018) Revision 3.3
[1] Fixed fatal bug in "flags" due to careless previous modifications. All 32-bit stack versions

What to download (you need to log-in to be able to see the attachments below). The attachments are now NASM-only.
[1] baselib.zip (BASELIB sources and binaries). Linux. Win. 32-bit and 64-bit
[2] cpulib.zip (CPULIB source and binaries). Not compatible with BASELIB. Linux. Win. 32-bit and 64-bit

All three of the above are standalone.

Note that this is NASM-only attachments. For other syntax of BASELIB you can download the full size of BASELIB/CPULIB from Sourceforge or from my Google+ profile.

You should ignore older attachments found in this thread or in other thread.
« Last Edit: February 22, 2018, 04:11:02 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #37 on: March 03, 2018, 08:19:29 PM »
(25.March.2018) Revision 3.3.1

What to download:
[1] baselibs.zip (BASELIB sources and binaries). Linux. Win. 32-bit and 64-bit
[2] cpulib.zip (CPULIB source and binaries). Not compatible with BASELIB. Linux. Win. 32-bit and 64-bit

Each one of the above are standalone.

Download from:
1. BASELIBS project at ,sourceforge
2. my Google drive or
3. my Google+ profile.

You should ignore older attachments found in this thread or in other thread..
« Last Edit: April 09, 2018, 02:12:00 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #38 on: June 17, 2018, 08:10:22 AM »
(9.Aug.2018) Revision 3.5
This is release with major structural changes
[1] Bug fixes to "timer_start". All Win64 versions
[2] Use only GCC for sources and binaries linking.

What to download:
[1] baselibs.zip (BASELIB sources and binaries). Linux. Win. 32-bit and 64-bit
[2] cpulib.zip (CPULIB source and binaries). Not compatible with BASELIB. Linux. Win. 32-bit and 64-bit

Each one of the above are standalone. You should ignore older attachments found in this thread or in other thread. Download from attachment below or from either one of these sites (plus other syntaxes);

1) Sourceforge's baselibs 
2) Google Drive's BASEUPLOADS
« Last Edit: August 09, 2018, 10:12:06 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #39 on: August 19, 2018, 05:16:49 PM »
(22.Aug.2018) Revision 4.0.1
[1] Bugfix: prnxmm/dumpxmm. All 32-bit versions.

What to download:
[1] baselibs.zip (BASELIB sources and binaries). Linux. Win. 32-bit and 64-bit
[2] cpulib.zip (CPULIB source and binaries). Not compatible with BASELIB. Linux. Win. 32-bit and 64-bit
[3] fullbinaries.zip. For other binaries not included in [1] and [2] such as .DLL and .SO. Using linkers other than GCC (LINK, ld, Golink).

Each one of the above are standalone. You should ignore older attachments found in this thread or in other thread. Download from attachment below or from either one of these sites (plus other syntaxes);

1) Sourceforge's baselibs 
2) Google Drive's BASEUPLOADS

Hope you'll find them helpful and useful in your learning experience

Regards
« Last Edit: August 22, 2018, 11:29:34 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #40 on: September 22, 2018, 11:14:17 AM »
I've been getting requests to come up with BigInt/Num routines for this library in NASM. But I have no intention to provide such support in the current distribution. My personal opinion is that 64-bit numbers are big enough for most beginners' purposes. 128-bit seems to be out of this world. So, my apologies, there will be no such support provided by BASELIB. But for those who really (like really really) need 128-bit integers, I can give you two makeshift routines that deal with such conversion.

Code: [Select]
;-------------------------------
;prnint128(1)
;Display signed 128-bit integer
;-------------------------------
;RAX    : Address of a 128-bit to display
;-------------------------------
;Ret    : -
;Note   : Two consecutive QWORDS in memory
;         that sum up to the 128-bit value
;-------------------------------
align 8
prnint128:
        push    rax
        push    rbx
        push    rcx
        push    rdx
        push    rsi
        push    rdi
        push    r8
        sub     rsp,64
        mov     ecx,10
        xor     r8d,r8d
        mov     rbx,[rax+8]
        mov     rsi,[rax]
        bt      rbx,63
        jnc     .plus
        cmp     rsi,0
        jne     .mm
        neg     rbx
        jmp     .nn
.mm:    not     rbx
.nn:    not     rsi
        mov     r8d,1
        add     rsi,1
.plus:  mov     rdi,rsp
        add     rdi,63
        mov     byte[rdi],0
.again: xor     edx,edx
        mov     rax,rbx
        div     rcx
        mov     rbx,rax
        mov     rax,rsi
        div     rcx
        mov     rsi,rax
        add     dl,30h
        sub     rdi,1
        mov     byte[rdi],dl
        mov     rax,rbx
        or      rax,rsi
        jnz     .again
        test    r8,r8
        jz      .done
        sub     rdi,1
        mov     byte[rdi],'-'
.done:  mov     rax,rdi
        sub     rsp,32
        call    prnstrz
        add     rsp,96
        pop     r8
        pop     rdi
        pop     rsi
        pop     rdx
        pop     rcx
        pop     rbx
        pop     rax
        ret
;-------------------------------
;prnint128u(1)
;Display Unsigned 128-bit int
;-------------------------------
;RAX    : Address of a 128-bit to display
;-------------------------------
;Ret    : -
;Note   : Two consecutive QWORDS in memory
;         that sum up to the 128-bit value
;-------------------------------
align 8
prnint128u:
        push    rax
        push    rbx
        push    rcx
        push    rdx
        push    rsi
        push    rdi
        sub     rsp,72
        mov     ecx,10
        mov     rbx,[rax+8]
        mov     rsi,[rax]
        mov     rdi,rsp
        add     rdi,63
        mov     byte[rdi],0
.again: xor     edx,edx
        mov     rax,rbx
        div     rcx
        mov     rbx,rax
        mov     rax,rsi
        div     rcx
        mov     rsi,rax
        add     dl,30h
        sub     rdi,1
        mov     byte[rdi],dl
        mov     rax,rbx
        or      rax,rsi
        jnz     .again
        mov     rax,rdi
        sub     rsp,32
        call    prnstrz
        add     rsp,104
        pop     rdi
        pop     rsi
        pop     rdx
        pop     rcx
        pop     rbx
        pop     rax
        ret

You can add these two in "base64w/base64x" sources and try to play around with it. Like for example;

Code: [Select]
        ;default rel
        global main

        section .data align=32
msg:    db 'Hello World',0ah,0
y:      dq -1,-1

        section .text
main:
        sub     rsp,40

        mov     rax,y
        call    prnint128
        call    prnline
        mov     rax,y
        call    prnint128u

        add     rsp,40
        ret

You can try using instructions like ADCX and MULX to test the accuracy of the functions above (for example, adding two 64-bit registers, without carry).

Good luck.
 
EDIT: Replace the divisor with 8 for octal, and 16 for hex. Now u can get 4 more extra routines for signed and unsigned hex and octal.
« Last Edit: September 22, 2018, 11:19:31 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #41 on: September 22, 2018, 11:33:28 AM »
For unsigned Hex, both 512-bit and 256-bit...
I don't know if this works because I have no calculators big enough for verification purposes.. ;D

Code: [Select]
;-------------------------------
;prnhex512u(1)
;Display Unsigned 512-bit Hex
;-------------------------------
;RAX    : Address of a 512-bit to display
;-------------------------------
;Ret    : -
;Note   : Eight consecutive QWORDS in memory
;         that sum up to the 512-bit value
;-------------------------------
align 8
prnhex512u:
        push    rax
        push    rbx
        push    rcx
        push    rdx
        push    rsi
        push    rdi
        sub     rsp,128
        mov     rsi,rax
        mov     rbx,[rsi+56]
        mov     rdi,rsp
        cld
        mov     ecx,16
.msb:   xor     eax,eax
        shld    rax,rbx,4
        add     al,30h
        cmp     al,'0'
        jz      .nope
        cmp     al,'9'
        jbe     .ok
        add     al,7
.ok:    stosb
.nope:  shl     rbx,4
        sub     ecx,1
        jnz     .msb
;-------------------------
        mov     edx,7
.more:  sub     rsi,8
        mov     ecx,16
        mov     rbx,[rsi+56]
.next:  xor     eax,eax
        shld    rax,rbx,4
        add     al,30h
        cmp     al,'9'
        jbe     .ok1
        add     al,7
.ok1:   stosb
.nope1: shl     rbx,4
        sub     ecx,1
        jnz     .next
        sub     edx,1
        jnz     .more
        xor     al,al
        stosb
        mov     rax,rsp
        sub     rsp,32
        call    prnstrz
        add     rsp,160
        pop     rdi
        pop     rsi
        pop     rdx
        pop     rcx
        pop     rbx
        pop     rax
        ret
;-------------------------------
;prnhex256u(1)
;Display unsigned 256-bit Hex
;-------------------------------
;RAX    : Address of a 256-bit to display
;-------------------------------
;Ret    : -
;Note   : Four consecutive QWORDS in memory
;         that sum up to the 256-bit value
;-------------------------------
align 8
prnhex256u:
        push    rax
        push    rbx
        push    rcx
        push    rdx
        push    rsi
        push    rdi
        sub     rsp,72
        mov     rsi,rax
        mov     rbx,[rsi+24]
        mov     rdi,rsp
        cld
        mov     ecx,16
.msb:   xor     eax,eax
        shld    rax,rbx,4
        add     al,30h
        cmp     al,'0'
        jz      .nope
        cmp     al,'9'
        jbe     .ok
        add     al,7
.ok:    stosb
.nope:  shl     rbx,4
        sub     ecx,1
        jnz     .msb
;-------------------------
        mov     edx,3
.more:  sub     rsi,8
        mov     ecx,16
        mov     rbx,[rsi+24]
.next:  xor     eax,eax
        shld    rax,rbx,4
        add     al,30h
        cmp     al,'9'
        jbe     .ok1
        add     al,7
.ok1:   stosb
.nope1: shl     rbx,4
        sub     ecx,1
        jnz     .next
        sub     edx,1
        jnz     .more
        xor     al,al
        stosb
        mov     rax,rsp
        sub     rsp,32
        call    prnstrz
        add     rsp,104
        pop     rdi
        pop     rsi
        pop     rdx
        pop     rcx
        pop     rbx
        pop     rax
        ret

If it works, it works. If don't, don't bother. For example;
Code: [Select]
        ;default rel
        global main

        section .data align=32
msg:    db 'Hello World',0ah,0
y:      dq -1,-1,-1,0ABCDEFh,-1,-1,-1,0x7fffffffffffffff

        section .text
main:
        sub     rsp,40

        mov     rax,y
        call    prnhex512u

        add     rsp,40
        ret

Output

Code: [Select]
7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000ABCDEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Offline stressful

  • Full Member
  • **
  • Posts: 101
  • Country: 00
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #42 on: Today at 03:59:33 AM »
Ok now once you have included "prnint128 and prnint128u" in your base64w / base64x sources, you can for example use them to extend calculations beyond 64-bit limitation.

Using ADCX:
Code: [Select]
main:
        sub     rsp,40
        mov     rax,-1  ;maxed-out RAX
        mov     rbx,2   ;add 2
        adcx    rax,rbx
        jc      .carry
        push    0
        push    rax
        mov     rax,rsp
        call    prnint128u
        add     rsp,56
        ret
.carry:
        push    1
        push    rax
        mov     rax,rsp
        call    prnint128u
        add     rsp,56
        ret

Result should be: 18446744073709551617 instead of 1 as in normal saturated arithmetic in 64-bit world.

Now using MULX:
Code: [Select]
        section .text
main:
        sub     rsp,40
        mov     rcx,-1  ;maxed-out RCX
        mov     rdx,2   ;mul 2
        mulx    rax,rbx,rcx
        push    rax
        push    rbx
        mov     rax,rsp
        call    prnint128u
        add     rsp,56
        ret

Result should be: 36893488147419103230 (as in 18446744073709551615 x 2).

Just to remind you that these are unofficial extensions and not part of core BASELIB. Maybe someday I'd consider adding BigInt routines in there.

Good luck and enjoy your weekend.

 
« Last Edit: Today at 04:04:46 AM by stressful »