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  Next
Author
Thread Post new topic Reply to topic
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I changed the library from source to PDF format so that it is more presentable and more appropriate in educational environment. Not trying to be a dumb ass, but at least a beginner should show the effort & motivation to type them up into a text editor and digest it slowly Very Happy

A few changes from the last upload:

1. Name changes to certain I/O routines

2. Win versions now are using printf for string processing rather than relying on putchar to print byte streams. I reluctant to do this at first but putchar is too slow to print a repetitive strings if compared to the Linux version.

3. Some routines were deleted.

4. Keyboard input routines (readstr, readstrz, readdbl, readint) now have error handling to prevent programs from going crazy if a user inputs nothing but the Enter key.

5. DOS 16-bit versions use different interrupt calls for basic I/O if compared to the previous ones. This one is a bit more complicated but is much richer in interrupt usage, plus it prints string a bit faster.

The library is not perfect, but should be okay for most beginners purposes.

Good day.
Post 17 Jan 2016, 17:20
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Jan 19th Update (version 1,0.12)

[+] added "isint" routine to check whether a floating point value is also an integer.
[+] Added new information to the Doc on how to create a minimum working source without the need to write them all at once.
Post 18 Jan 2016, 18:02
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 790
Location: Jakarta, Indonesia
Hi fasmnewbie,

A small suggestion.
For convenience, maybe a zipped source format is preferable to copy pasting from PDF.

Smile
Post 19 Jan 2016, 01:53
View user's profile Send private message Reply with quote
idle



Joined: 06 Jan 2011
Posts: 346
Location: ukraina
indeed, searching souls must be inspired, clean book does little motivation
Post 19 Jan 2016, 16:02
View user's profile Send private message Send e-mail Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I uploaded the second attachment (CORE.zip) containing all the source files that can be used immediately.
Post 31 Jan 2016, 23:41
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I didn't include this in the attachment but u can include this yourself with no problem. This simple routine demonstrates how u can use 'subtle' routines like chr_find to create other routines - to prove that assembly language is just as powerful as any HLL in terms of modularity and software reusability.

The routine below str_token tries to simulate the C's strtok, by using chr_find routine as its main processor engine (slow, but sure) Very Happy


Code:
format PE64 console
include 'win64axp.inc'

;practice here
mov rbx,delimiter
mov rax,thestring
call str_token

call exitp
;data goes here
thestring db '--,  a drunk ? trying to -? write..',0
delimiter db '-,. ?',0



output:

Code:
a
drunk
trying
to
write



The routine;

Code:
;------------------------------
;str_token(2)
;Display tokens off a 0-ended string
;------------------------------
;RBX    : Addr of 0-ended delimiter string
;RAX    : Addr of the string
;------------------------------
align 8
str_token:
        push    rax
        push    rcx
        push    rdx
        push    rsi
        mov     rsi,rax
        mov     rcx,1
.againlodsb
        cmp     al,0
        je      .done
        mov     dl,al
        call    chr_find  ;main processor
        test    rax,rax
        jnz     .hit    ;delimiter hit
        xor     rcx,rcx
        mov     al,dl
        call    prnchr
        jmp     .again
.hit:   test    rcx,rcx
        jnz     .again
        call    newline
        add     rcx,1
        jmp     .again
.done:  pop     rsi
        pop     rdx
        pop     rcx
        pop     rax
        ret



For 32-bit and 16-bit versions, just replace the registers (RAX->EAX etc)
Post 03 Feb 2016, 19:20
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Added basic file processing routines to 64-bit library(fnew, fopen, fread, fwrite, fclose and fsize). Nothing fancy. Revision 1.1.6
Post 15 Feb 2016, 04:17
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Added str_find routine to both 32 and 64 bit source. Return status in RAX.

Example:

Code:
format ELF64 executable 3

mov rbx,thekey
mov rax,thestring
call str_find
call prnint      ;Display return status

call exit
thestring db 'Find me if u can!',0
thekey db 'd me i',0

Post 16 Feb 2016, 11:11
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Routines "memview" and "stackview" are very useful if you want to see the content of a C's structs and what's hidden in there, particularly on Linux where everything is defined with C interface and assembly reference is scarce. For example, I used these two to figure out the data kept in struct timespec (for nanoseconds) and file size field from fstat.

Use with caution.
Post 16 Feb 2016, 11:27
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I added prnstr32 routine to core32w.asm to allow prnstr to work properly on native 32-bit Win OSes. I've been testing the lib on 64-bit OS, so I might have missed some of the technical issues related to native Win32 OSes. So if you are on native 32-bit Win, use prnstr32 in place of prnstr to print string with size. If condition so requires, just delete prnstr routine and rename prnstr32 to prnstr.

Linux source and 64-bit version are not affected by this.
Post 18 Feb 2016, 07:38
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Made lots of changes;

1. Got rid of routine redundancies
2. introduced "chr_shuffle" to shuffle a 0-ended string randomly.
3. aprnint, aprndbl... now takes 3rd argument so you can decide on your own separator to print the array (newline, space, etc)
4. rndigit now takes a slightly different role (return a small random integer instead of digit... up to 255). You can increase/decrease the max bound in the same routine.

This is revision 1.1.10.
Post 21 Feb 2016, 11:30
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Added str_append and str_appends, to enable 2 strings to be combined.
Post 26 Feb 2016, 12:59
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
I wrote a small demo program to test the mem_alloc behaviour among other things. I can no longer test this on Linux because Win10 ate up my reFind/grub entries. So I hope u can help me test on Linux and other PCs since I am not quite convinced with this mem_alloc routines I am having (malloc for windows and sys_brk for Linux).

The code tries to copy a user-input list of doubles to a dynamic memory block allocated by mem_alloc and then sort them up before displaying them.

I tested this using my DLL but it should also work on the naked version library downloadable on Page 1.


Code:
format PE64 console
include 'win64axp.inc'
;include 'base6.inc' ;tested via DLL
entry main

SZE = 4096

section '.data' data readable writeable
instr db 'input doubles, ''.'' to end: ',0ah,0
dblstr rb 40

section '.code' code readable executable
main:
        mov     rax,instr
        call    prnstrz
        mov     rax,SZE
        call    mem_alloc       ;request memory
        mov     rsi,rax         ;save block pointer
        xor     rdx,rdx         ;data count
        xor     rdi,rdi         ;index addressing
.get:   mov     eax,'> '        ;get doubles
        call    prnstreg
        mov     rax,dblstr
        call    readstrz
        cmp     byte[dblstr],'.' ;sentinel
        je      .done
        mov     rax,dblstr
        call    str2dbl
        add     rdx,1
        mov     [rsi+rdi],rax   ;save to dynamic mem
        add     rdi,8
        jmp     .get
.done:  cmp     rdx,0
        je      .exit
        mov     rax,rsi
        mov     rbx,rdx
        mov     rcx,0           ;ascending
        call    sort_dbl        ;sort data
        mov     rax,rsi
        mov     rcx,'|'
        mov     rbx,rdx
        call    aprndbl         ;display sorted array
.exit:  mov     rax,rsi
        call    mem_free        ;free mem
        call    exitp



Output


Code:
input doubles'.' to end:
234.112
> -0.0
115.238
> -0.012
0.0
64.345
.
-0.012|-0.0|0.0|64.345|115.238|234.112|

Post 26 Feb 2016, 13:09
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
[Attachment moved to Page 1]


Last edited by fasmnewbie on 01 Mar 2016, 05:24; edited 1 time in total
Post 28 Feb 2016, 14:42
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
[Attachment moved to Page 1]


Last edited by fasmnewbie on 01 Mar 2016, 05:25; edited 1 time in total
Post 28 Feb 2016, 14:45
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
If you are in doubt on which one to use, pick the downloads on page 1. But at later stages, you might want to use the DLL and object versions, which offer better information hiding and much more convenience.

I think this is my final token. Thrown in everything I have. Not too good, but not too bad either. Not too fast, but not too slow too! LOL. But good enough for most beginners purposes. Hope you like it Smile
Post 28 Feb 2016, 14:54
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Corrected fatal bugs with the 32-bit versions/sources because I been ignoring them all these long to focus on 64-bit versions. LOL.

1. flt2int. replaced finit with multiple fstp because that would otherwise neutralize fpu_round (to set the rounding mode) result that should be called prior to calling flt2int.

2. powint. Forgot to include when the pow is 0. fixed

3. sincos. I don't know how the call to fpu_stack ended up somewhere in there. fixed

4. prnxmm / dumpxmm - delete option #9.

5. str_findz (all versions incl. 64-bit) now return the same result as str_find (location, instead of just status).

If there's no more bugs, then take this as the final version/revision.
Post 15 Mar 2016, 15:44
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Just uploaded the Linux 64-bit SO version (similar in concept to Win64 DLL just in case u don't know what it is).

Since Linux implementations may vary, do alert me if it behaves differently on your Linux. This SO was compiled with Mint 17.
Post 16 Mar 2016, 08:56
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
Apparently there's one very minor bug to str_findz. Fixed.

Name change: sort_chr to sort_byte to reflect the general idea of sorting not just ascii character array but also byte array.

Just like always, if there's no more fatal bugs, this is the final revision. Sorry for the many corrections and updates. Good luck.
Post 17 Mar 2016, 15:04
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 421
While using this library, there are certain technical issues that you should be aware of;

1. All strings write are forward. If u attempt to write backward direction, and use one of the routine with CLD in it then you maybe in trouble.

2. Stack allocation for routines are not guaranteed to be empty (Try use stackview and you'll see that the stack frame content are not entirely empty). So FXSAVE data may land on one of these polluted area and be restored (FXRSTOR) with other 'residues' from the polluted area. I haven't encountered any problems so far but sh1t happens that could for example mess up with masking data.

So my advice is that if u do need a guaranteed result, you need to alter all the routines with FXSAVE in it by clearing the data (use mem_reset) before calling FXSAVE.

Like;


Code:
    push rbp
    mov rbp,rsp
    sub rsp,528          ;declare more instead of just 512
    and rsp,-16          ;align it first
    mov rbx,528         ;number of bytes to be cleared 
    mov rax,rsp
    call mem_reset       ;clear the memory
    fxsave [rsp]        ;then use FXSAVE to place data in there
    ...
    ...
    fxrstor [rsp]
    mov rsp,rbp
    pop rbp
    ret



3. Routines support no AVX because my laptop don't have AVX. My work PCs do have them but I don't understand AVX anyway. But I may add them in the future xD
Post 19 Mar 2016, 14:25
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  Next

< 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.