flat assembler
Message board for the users of flat assembler.

 Index > Examples and Tutorials > BASELIB: General purpose libs for beginners Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Author
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
@Revo

Noted.
02 Jun 2015, 20:36
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I added 2 more library for 32-bit version (win and linux). I 'adopted' them straight from the 64-bit versions, so could be buggy somewhere.

Plus, I updated the fnotes into PDF format because I didn't expect DOS and Linux text presentation to differ that much.

Due to its low-levelness, these routines are highly usable in NASM, except that floating point part. (You need to convert them to hex first. Use prthex routine to get their Hex equivalences). Example;

mov rax,10.0 is changed to mov rax,0x4024000000000000

Example using prthexu routine (unsigned) to get a FP equivalence;

Code:
```mov rax,1.0
call prthexu       ;Display and equals to 0x3FF0000000000000
mov eax,10.0
call prthexu       ;Display and equals to 0x41200000    ```

Replace all those FP immediates with their Hex equivalences. Better still, why not use FASM instead.

Good luck and may the force be with you

Last edited by fasmnewbie on 06 Jul 2015, 14:14; edited 1 time in total
02 Jun 2015, 20:52
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
prtdbl is a 64-bit precision string conversion of raw FP data. It offers no precision or rounding control. Some motivated beginners may attempt their own precision or rounding policy over a FP data. Below is a routine that can offer more control to precision and rounding by splitting a double precision data to integral and precision part to make it easier to do just that.

Code:
```;------------------------------
;##     @dblsplit(1)/2
;       Split a double into two parts
;       Return integral in RAX
;       Return fraction in RBX
;------------------------------
dblsplit:
push    rdx
sub     rsp,140          ;local data space
mov     [rsp],rax        ;copy
mov     [rsp+8],rax
lea     rdx,[rsp+16]     ;Save FPU
fsave   [rdx]            ;
mov     rbx,rax
xor     rdx,rdx          ;Negative marker
finit                    ;Clear FPU
fstcw   [rsp+138]        ;
bts     word[rsp+138],10 ;Round down
fldcw   [rsp+138]        ;
fabs                     ;Absolute it
test    rax,rax          ;If negative,
jns     .nope
mov     rdx,1            ;Marker up
.nope:
fld     qword[rsp+8]     ;load the same value
fabs
fistp   qword[rsp+8]     ;turn it to integer
fsub    st1,st0          ;Substract true vs integral
fstp    qword[rsp]       ;Save to integral
fabs
fstp    qword[rsp+8]     ;Remainder as fraction
mov     rax,[rsp]        ;integral part
mov     rbx,[rsp+8]      ;fraction part
cmp     rdx,1
jne     .done
bts     rax,63           ;Negate it if negative
.done:
lea     rdx,[rsp+16]     ;Restore FPU
frstor  [rdx]
pop     rdx              ;
ret    ```

This routine will return the integral part in RAX and the fraction part in RBX. Beginners can start working on each different part separately and combine the result to form a FP representation with more customized precision and rounding control.

Please note that this works only normal FP data.

Sample usage:
Code:
```mov rax,-45.3234
call dblsplit
;now RAX contains -45.00
;RBX contains 0.3234    ```

Hope this would be useful.

p/s This is not part of the attached routines. You can add this routine to the current one without any problem (to Linux and Win version).

Happy coding.
08 Jun 2015, 08:05
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I updated the library (June 13th, 2015). Deleted some, added some other routines. Please test the prtxmm/dumpxmm routine because my knowledge of SSE instructions is quite limited at the moment. prtxmm/dumpxmm now supports byte-level integer instructions (hopefully). Thanks and be cool.
13 Jun 2015, 14:33
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
June 16th,2015

I updated the flib today for my midnite coding routine. Nothing fancy. Just introduced another routine and patching up here and there. I think this is the final revision for flib.
15 Jun 2015, 18:18
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie

I uploaded FCORE.zip as the replacement for FLIB. I introduced "sleep" function in this release. Also added "prtdble", a prtdbl extension that takes decimal places as the 2nd argument in RBX.

I think this library is complete now and has almost every routines required by beginners to start learning assembly programming quite comfortably.

18 Jun 2015, 11:54
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
A little correction to function "sleep" for Linux version. Just a line of code;

Code:
```;--------------------------------
;#42    : sleep(1)
;OBJ    : Put execution at sleep
;--------------------------------
;RAX    : Milliseconds (unsigned)
;               > reg64,imm,[dq]
;--------------------------------
;NOTE   : 1000ms = 1s.
;RETN   : -
;--------------------------------
sleep:
push    r11
push    rdi
push    rsi
push    rax
push    rcx
push    rdx
mov     rdi,1000000     ;convert to nano
mul     rdi
cmp     rax,1000000000  ;if > 1s
jae     .over
push    rax
push    0
jmp     .ok
.over:
xor     rdx,rdx
mov     rcx,1000000000 ;<---- change to this one
div     rcx
and     rdx,0xffff
push    rdx             ;nano
push    rax             ;sec
.ok:
lea     rsi,[rsp+8]     ;nano
lea     rdi,[rsp]       ;sec
mov     rax,35
syscall
pop     rdx
pop     rcx
pop     rax
pop     rsi
pop     rdi
pop     r11
ret    ```

Sorry for the inconvenience.
19 Jun 2015, 03:52
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I added prtregd (display register in decimal) and str2flt. Also included alternatives to getdbl, getflt and getint routines. I think those are the last additions to the current library and I am not maintaining the library anymore. Hope those are enough for beginners. Enjoy it and happy coding.
20 Jun 2015, 12:46
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
This morning I realized that this library is missing one extremely important component - the ability to view the content of the stack as the code progresses. So I updated the main zip (FCORE.zip) to include this important piece of routine which is a must-have. The routine is stackview and stackviewd to display its content in decimal. One Linux example (Windows version should behave the same);

Code:
```format ELF64 executable 3

call stackview  ;view stack
call newline
push 35h        ;push one item
mov rax,4       ;traverse 4 items
call stackview  ;view stack
call newline
push 34h        ;push another item
mov rax,4       ;traverse 4 items
call stackview  ;view stack
call newline
pop rcx         ;Pop one item
mov rax,4       ;view 4 items at once
call stackview  ;Display reduced stack.
call newline
mov rax,rcx     ;confirm RCX contains the last item popped
call prthex

call exit    ```

With the following output (stack grows downwards). The (*) is the TOS and the value in [..] is the stack address.

Code:
```[00007FFF32C5EB68] 00007FFF32C6097E
[00007FFF32C5EB60] 0000000000000000
[00007FFF32C5EB58] 00007FFF32C60975
[00007FFF32C5EB50] 0000000000000001*

[00007FFF32C5EB60] 0000000000000000
[00007FFF32C5EB58] 00007FFF32C60975
[00007FFF32C5EB50] 0000000000000001
[00007FFF32C5EB48] 0000000000000035*

[00007FFF32C5EB58] 00007FFF32C60975
[00007FFF32C5EB50] 0000000000000001
[00007FFF32C5EB48] 0000000000000035
[00007FFF32C5EB40] 0000000000000034*

[00007FFF32C5EB60] 0000000000000000
[00007FFF32C5EB58] 00007FFF32C60975
[00007FFF32C5EB50] 0000000000000001
[00007FFF32C5EB48] 0000000000000035*

34    ```

Sorry for the late addition. I didn't realize it until today. This offers a slight advantage over the the debugger because beginners can see the effect immediately as the coding progresses.
26 Jun 2015, 09:41
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
People might ask why I am creating this library...

The answer is simple: I'd love to see FASM gains much more acceptance in learning institutions. By having a simple library like this, decision makers would be encouraged to adopt FASM as their main assembler for related courses in their institutions. And MASM or NASM-infested instructors now can easily compare codes and understand / appreciate FASM much easier for adoption.

I know the chance is slim due to lack of FASM-speaking reference materials. But with this very small small step, who knows what lies ahead. There are fence-sitters out there who still can't decide which assembler to use.

But my code is in poor condition. Hope others can help to make it better and and on par with other similar library (in other ASM language) which I found are far more advanced.

So I hope I did not harm to FASM as a whole with this small small effort.

Until next time.
29 Jun 2015, 22:38
redsock

Joined: 09 Oct 2009
Posts: 353
Location: Australia
redsock
I had/have similar hopes with my own x86_64 library (https://2ton.com.au/HeavyThing/) and since I GPLv3'd it early this year, I must say there are far fewer people interested in "assembler made easy" than I originally thought there were.

I like your library's simplicity of covering the bases and wish you luck toward your goal in any case

Cheers
30 Jun 2015, 07:05
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I added 2 more routines (fpdinfo, fpfinfo) to enable beginners to analyze floating point binary for double and float.

I updated the Documentation to enable beginners to use Section and Segment instead of the default flat model.

I modified stackview/d/u so that the address is shown on the right side of the data. This makes more sense in terms of bytes addressing and offset, for example, [rbp-1] would be confusing if I placed the address on the left (Most Significant) side.

I tested the code quite thoroughly and found no more fatal / non-fatal bugs. If you found one, you can write it here and I'll see what I can do.

Until a bug is found, this is the final version of FCORE.

Good luck.
04 Jul 2015, 04:36
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
Fixed a minor bug in fpfinfo.
08 Jul 2015, 17:53
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
There's a thread by Picnic if beginners wish to extend their knowledge on console-based graphics and other cool stuff.

http://board.flatassembler.net/topic.php?t=15394
01 Aug 2015, 03:14
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I uploaded a slightly improved versions with some minor additions of routines that I think are necessary for beginners. In addition, I also included the 32-bit versions of the library just in case someone need it. The 32-bit versions are direct adoption from its 64-bit siblings.

Some of the new routines introduced;

Considering the facts that beginners often mistakenly use ADD, MUL, SUB and DIV for floating point use instead of integers. These would softly introduce them to floating point operations before even actually learning about SSE and FPU.

2. sse_round and fpu_round
Allow users to set the rounding mode of SSE and FPU.

3. factorial, log10 and ln10

I think that's it for now. The zip file is EASY64.zip found at the first post.

Enjoy.
22 Sep 2015, 09:14
TmX

Joined: 02 Mar 2006
Posts: 820
Location: Jakarta, Indonesia
TmX
Thanks fasmnewbie.
Gotta learn this stuff on weekend
22 Sep 2015, 09:57
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I missed the dblsplit update due to some mixed up in source files. The old one isn't wrong but this is more compact and shorter. It's applicable to both 64-bit versions.

Code:
```;------------------------------------
;dblsplit(1)/2
;Split a double into parts
;------------------------------------
;RAX    : The FP value to split
;------------------------------------
;Ret    : Integral part in RAX
;       : Fraction part in RBX
;Note   : Value in should in FP format
;       : Deals normal FP value only
;------------------------------------
dblsplit:
push    rbp
mov     rbp,rsp
sub     rsp,512
and     rsp,-16
fxsave  [rsp]
call    fpu_round.zero
push    rax
fld     qword[rsp]
fld     qword[rsp]
frndint
fsub    st1,st0
fstp    qword[rsp]
mov     rax,[rsp]
fabs
fstp    qword[rsp]
pop     rbx
fxrstor [rsp]
mov     rsp,rbp
pop     rbp
ret    ```

Last edited by fasmnewbie on 24 Sep 2015, 12:35; edited 1 time in total
23 Sep 2015, 15:15
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
TmX wrote:
Thanks fasmnewbie.
Gotta learn this stuff on weekend
You, learning from my codes? I don't buy it. Not a chance. Lu pura2 jadi newbie je tu bro. ;D
23 Sep 2015, 15:19
fasmnewbie

Joined: 01 Mar 2011
Posts: 553
fasmnewbie
I updated the zip file to reflect a recent addition of fpu_tag so that a user can quickly verify an FPU register's status quickly. This is actually an information from the FPU's Tag word register.

Example;

Code:
```format PE64 console
include 'win64axp.inc'

finit           ;Init FPU
call fpu_tag   ;View default Tag status
call fpu_tag  ;See the status of the Tag word
call exitp     ```

Will produce this output

Code:
```T7 T6 T5 T4 T3 T2 T1 T0
11 11 11 11 11 11 11 11=FFFF
T7 T6 T5 T4 T3 T2 T1 T0
00 01 11 11 11 11 11 11=1FFF    ```

I think that's the final addition of routine to the library. I can't think of any other basic routine that can further help beginners going through those 'difficult' phases in learning assembly programming. I think 110 routines for 64-bit version and 109 for 32-bit versions are more than enough. IMO, this is the most complete native assembly library routine one can find from the internet, although not the best.

Anyway, good luck and happy coding.
25 Sep 2015, 19:34
JohnFound

Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
fasmnewbie wrote:
I updated the zip file to reflect a recent addition of...

Isn't it better to adopt some version control system. This way all changes will be easily followed and the history will be saved as well.

I would suggest using Fossil scm, because it has the same minimalistic spirit as assembly programming itself.

You can host your repositories free at Chisel or on your own hosting (with fossil it is very easy).

_________________