NASM - The Netwide Assembler

NASM Forum => Example Code => Topic started by: stressful on September 14, 2016, 02:34:22 PM

Title: BASELIB: General Purpose Lib for Beginners
Post by: stressful on September 14, 2016, 02:34:22 PM
I make a new thread because the old one is too messy and misleading

Attached is 6 source files and 6 binaries that can be used to facilitate learning of NASM, intended for beginners.

Source format (You can start practicing straight out of the box)
Code: [Select]
1. base32x.asm - Source for Linux32.
2. base32w.asm - Source for Win32
3. base64w.asm - Source for Win64.
4. base64x.asm - Source for Linux64.
5. sbase32x.asm - Source for Linux32 (stack version)
6. sbase32w.asm - Source for Win32 (stack version)

Binaries format
Code: [Select]
1. base3.o - the 32-bit library for Linux. Static library
2. base6.o - the 64-bit library for Linux. Static library.
3. base3.dll - same as 1) but for Windows. Dynamic
4. base6.dll - same as 2) but for Windows. Dynamic
5. sbase3.o - 32-bit lib, stack version for Linux. Static
6. sbase3.dll - 32-bit lib, stack version for Windows. Dynamic

Each folder has their own readme.txt and sample files demonstrating how to use the binaries properly. These libs are extremely low level and designed from instruction sets point of view. Not many high-level constructs and no C library is harmed while making these libs. Just plain instructions and minumum syscalls. CAUTION: This lib is not optimized and not intended for purposes other than as learning/helper tools.     

Hope u like it. Cheers and happy learning.

If u found any bugs or have any suggestions, do contact me. e-mail is in the Docs.

Revision  Nov 7th, 2016)
Code: [Select]
.small fixes to "fpu_round" and "sse_round" (stack versions only).


LIST OF BASELIB ROUTINES
Code: [Select]
Notes   : The number in braces shows the # of arguments required       
        : /Followed by the return value(s) (if applicable)       
        : The list is for 64-bit versions only 
------------------------------------------------------------------
Routines(177+2)        Descriptions
------------------------------------------------------------------
prnreg(1)       - Display register (hex)
prnregd(1)      - Display register (decimal)     
prnregdu(1)     - Display register (unsigned decimal)     
dumpreg(1)      - Display register dump (hex)
dumpseg         - Display segment registers dump (hex)
stackview(1)    - Display stack
memview(2)      - Display memory dump   
memviewc(2)     - Display memory dump, emphasis on string 
mem_reset(2)    - Reset memory content 
mem_set(3)      - Fill up memory content   
mem_insertr(2)  - Insert register content into memory
mem_copy(3)     - Copy memory block
mem_load(1)/2   - Load a file to memory 
opcode(2)       - Display encoding between 2 labels 
opsize(2)/1     - Get code size between 2 labels. 
flags(1)        - Display RFLAG     
prnint(1)       - Display signed integer     
prnintu(1)      - Display unsigned integer     
prnhex(1)       - Display hexadecimal     
prnhexu(1)      - Display unsigned hexadecimal     
prnoct(2)       - Display octal     
prnoctu(2)      - Display unsigned octal     
prnbin(1)       - Display signed binary     
prnbinu(1)      - Display unsigned binary     
prnbinf(3)      - Display formatted binary     
prnbinb(1)      - Display binary with byte separators     
fpbin(1)        - Display 64-bit Floating Point binary     
fpbind(1)       - Display 32-bit Floating Point binary     
prndbl(1)       - Display 64-bit precision value
prndblr(1)      - Display 64-bit precision value (rounded)   
prndble(2)      - Display 64-bit precision with decimal places     
prndblx(1)      - Display 80-bit precision value     
prnflt(1)       - Display 32-bit float value
prnfltr(1)      - Display 32-bit float value (rounded)   
dblsplit(1)/2   - Split a real8 into parts 
fpdinfo(1)      - Display Double binary information     
fpfinfo(1)      - Display Float binary information     
dec2str(2)      - Convert signed decimal to 0-ended string       
dec2stru(2)     - Convert unsigned decimal to 0-ended string       
hex2str(2)      - Convert signed hex to 0-ended string
hex2stru(2)     - Convert signed hex to 0-ended string
bin2str(2)      - Convert signed binary to 0-ended string
bin2stru(2)     - Convert unsigned binary to 0-ended string
flt2str(2)      - Convert a float to 0-ended string
dbl2str(3)      - Convert double to 0-ended string
readint/1       - Get an integer from kboard
readdbl/1       - Get a double from kboard 
readflt/1       - Get a float from kboard 
fpu_stack       - Display FPU stack     
fpu_sflag       - Display FPU Status flag   
fpu_cflag       - Display FPU Control flag     
fpu_tag         - Display FPU Tag Word status     
fpu_reg(1)      - Display one FPU register     
fpu_copy(1)/1   - Copy a FPU register 
fpu_precision(1)- Set FPU precision   
fpu_round(1)    - Set FPU rounding control   
sse_round(1)    - Set SSE rounding control   
sse_flags       - Display SSE flags     
prnmmx(2)       - Display one MMX register       
dumpmmx(1)      - Display MMX dump       
prnxmm(2)       - Display one XMM register       
dumpxmm(1)      - Display XMM dump       
clearxmm        - Clear all xmm registers     
prnymm(2)       - Display one YMM register       
dumpymm(1)      - Display YMM dump       
clearymm        - Clear all ymm registers     
prnintd(2)      - Display 32-bit integer     
prnintw(2)      - Display 16-bit integer     
prnintb(2)      - Display byte integer     
str2int(1)/1    - Convert 0-ended string to integer 
str2dbl(1)/1    - Convert 0-ended string to double 
str2flt(1)/1    - Contert 0-ended string to float 
dbl2int(1)/1    - Convert double to integer 
int2dbl(1)/1    - Convert integer to double 
isint(1)/1      - Check if an FP is a qualified integer 
rndint(1)       - Generate 1 random integer
rand(1)/1       - Get one unsigned random int
randq(2)/1      - Generate unique random ints & save
factorial(1)/1  - Get factorial
powint(2)/1     - Calculate x ^ n (Integer) 
pow2(1)/1       - Calculate 2 ^ n (Integer) 
iseven(1)/1     - Check if a number is even 
isodd(1)/1      - Check if a number is odd 
bconv(2)        - Convert & display from normal bases to other bases     
bitfield(3)     - Extract bitfield of a given data
addf(2)/1       - Floating Point ADD     
subf(2)/1       - Floating Point SUB     
mulf(2)/1       - Floating Point MUL     
divf(2)/1       - Floating Point DIV     
sqroot(1)/1     - Get square root 
fcalc(3)/1      - Two-operands floating point calculator
rad2deg(1)/1    - Convert radian to degree
deg2rad(1)/1    - Convert degree to radian
sine(1)/1       - Get sine of a radian 
tangent(1)/1    - Get tangent of a radian 
cosine(1)/1     - Get cosine of a radian 
sincos(1)/2     - Get Sine and Cosine
atangent(1)/1   - Get arc-tangent of a radian. 
log10(1)/1      - Get a common log for a FP value
ln10(1)/1       - Get a natural log for a FP value     
pow(2)/1        - Calculate x ^ n (precision) 
chr_isdigit(1)/1- Check if a char byte is a digit
chr_isalpha(1)/1- Check if a char is an alphabet 
chr_islower(1)/1- Check if a char is a lower case 
chr_isupper(1)/1- Check if a char is an upper case
chr_change(3)   - Change a char from a 0-ended string
chr_chcase(1)/1 - Change a char's case 
chr_toupper(1)/1- Convert a char to uppercase 
chr_tolower(1)/1- Convert a char to lowercase 
chr_find(2)/1   - Find a char from a 0-ended string 
chr_count(2)/1  - Count a char from a 0-ended string 
ascii(1)        - Simple ascii byte conversion
chr_shuffle(1)  - Shuffle a 0-ended string
str_copy(3)     - Copy a string to another string     
str_length(1)/1 - Find string length.     
str_cmpz(2)/1   - Compare 2 0-ended strings 
str_cmps(3)/1   - Compare 2 strings, with size 
str_toupper(1)  - Up the case of a 0-ended string 
str_tolower(1)  - Lower the case of a 0-ended string 
str_reverse(1)  - Reverse a 0-ended string 
str_trim(2)     - Trim a 0-ended string     
str_wordcnt(1)/1- Word count of a 0-ended string     
str_token(2)    - Display tokens off a 0-ended string     
str_find(3)/1   - Find a sub-string from a 0-ended string 
str_findz(2)/1  - Find a sub-string from a 0-ended string 
str_append(5)/1 - Appends two 0-ended strings, with size 
str_appendz(4)/1- Appends two 0-ended strings 
sort_int(2)     - Sort integer 
sort_byte(3)    - Sort char/byte array 
sort_dbl(3)     - Sort double   
sort_dblx(3)    - Sort real10   
sort_flt(3)     - Sort integer   
digitprob(2)/1  - Extract digit from a signed integer
digitprobu(2)/1 - Extract digit from an unsigned integer
digithprob(2)/1 - Extract digit from a signed hex
digithprobu(2)/1- Extract digit from an unsigned hex
digitscan(2)/1  - Extract digit from a signed integer
digitscanu(2)/1 - Extract digit from an unsigned integer
digithscan(2)/1 - Extract digit from a signed hex
digithscanu(2)/1- Extract digit from an unsigned hex
digitcount(1)/1  - Extract digit from a signed integer
digitcountu(1)/1 - Extract digit from an usigned integer
digithcount(1)/1 - Extract digit from a signed hex
digithcountu(1)/1- Extract digit from an umsigned hex
aprnint(3)       - Display array of signed integers
aprnintu(3)      - Display array of unsigned integers
aprndbl(3)       - Disp array of doubles
aprnflt(3)       - Dspl array of floats
aprndblx(3)      - Display array of real10
halt            - Pause screen
prnspace        - Print a whitespace
prnline         - Print a new line
prnspaces(1)    - Print multiple whitespaces
prnlines(1)     - Print multipls lines
prnchrp(2)      - Display char pattern
prnchrs(1)      - Display a character from the stack
prnchar(1)      - Display a character variable     
prnstrd(2)      - Display delimiter-ended string
prnstreg(1)     - Display short string off RAX
readchr(1)      - Get a character and save to variable
;-----   OS SPECIFICS   -----
prnchr(1)       - Display char in AL
prnstr(2)       - Display string with size
prnstrz(1)      - Display 0-ended string     
readch/1        - Get a char from kboard. Return in AL
readstr(1)/1    - Get a string with size
mem_alloc(1)/1  - Get memory
mem_free(1)/1   - Release memory 
timer_start     - Start a timer (win64 only)     
timer_stop/1    - Stop a timer (win64 only)
delay(1)        - Delay execution in milliseconds
file_new(1)     - Create a new file
file_open(2)    - Open a file with options for read or write 
file_read(3)/1  - Read from an opened file 
file_write(3)/1 - Write to an opened file 
file_close(1)   - Close file handle 
file_size(1)/1  - Get file size 
file_copy(2)    - Copy a file to a new file
exitp           - Pause before exit to system
exitx           - Exit to system
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 02:56:54 PM
If you don't know how to start, take any example files and start with simple things and short codes. Or you can use the source. This one below is for Linux64 using "base64x.asm" source file;

Code: [Select]
;nasm -f elf64 base64x.asm
;ld base64x.o -o base64x
;./base64x

global _start

section .text
_start:
        mov rax,34h           ;your very first instruction ;D
        push 0                ;option to view the register dump as unsigned hex
        call dumpreg          ;See if you managed to put 34h to RAX register. If you did, congratulations!

        call exitx

The output should be:

Code: [Select]
RAX|0000000000000034 RBX|0000000000000000 RCX|0000000000000000
RDX|0000000000000000 RSI|0000000000000000 RDI|0000000000000000
RBP|0000000000000000 RSP|00007FFE5F85AB30 R8 |0000000000000000
R9 |0000000000000000 R10|0000000000000000 R11|0000000000000000
R12|0000000000000000 R13|0000000000000000 R14|0000000000000000
R15|0000000000000000 RIP|0000000000400085

Helper routines such as dumpreg, stackview, dumpxmm etc are very useful for you to build awareness around your code behaviour at all time until you are satisfied. Don't let the routine codes of dumpreg scare you. Just use the routine. You are probably not ready for the source code just yet. Just worry about your own code.

Practice makes perfect.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 03:36:12 PM
Another example of basic I/O program in Linux64. This example demonstrates extensive use of basic I/O routines offered by the library;

Code: [Select]
; Compile: nasm -f elf64 base64x.asm
; Link : ld base64x.o -o base64x
; Execute: ./base64x

global _start

section .bss
theName resb 100      ;allocation for large buffer
gend resb 1

section .data
hello db "Please input your name: ",0
ageStr db "How old are you?: "
hiMr db "Hi Mr ",0
hiMs db "Hi Ms ",0
gender db "Enter gender [m/f]: ",0
age dq 0

section .text
_start:
mov rax,gender ;ask for gender
call prnstrz
call readch ;get the char
call halt ;hold the screen
mov [gend],al ;copy pressed key into a byte var

mov rax,hello ;ask for name
call prnstrz
mov rax,theName ;read the string to buffer
call readstr ;return 0-ended string buffer

mov rbx,18 ;size of string
mov rax,ageStr ;the string to print. Ask for age
call prnstr ;note, this is prnstr, not prnstrz
call readint ;read integer from keyboard. Return in RAX
mov qword[age],rax  ;save the result to a variable

mov rax,hiMs ;Decision for salutation. Default case
cmp byte[gend],'f'
je .female        
mov rax,hiMr ;Else

.female:
call prnstrz
mov rax,theName
call prnstrz
mov rax,',you`re '
call prnstreg
mov rax,qword[age]
add rax,1
call prnintu
mov rax,' next ye'
call prnstreg
mov rax,'ar'
call prnstreg

call exitx

Hopefully, with this possible output;

Code: [Select]
Enter gender [m/f]: m
Please input your name: Frank Kotler
How old are you?: 34
Hi Mr Frank Kotler,you`re 35 next year

This example maybe too much for a beginner, but this should give you the basic idea of many things of assembly language, this library and NASM (variables, sections, registers, call instructions, basic anatomy etc).
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 03:58:58 PM
I am trying to give as many examples as possible before I leave  ;D

This example shows how you can save lots of time in trying to grab the idea of an x86 stack (TOS, grows downwards, shrink upwards, push pop etc etc).

Code: [Select]
The stack (assumed) empty...
00007FFC2F5AB3AC |00007FFC2F5A9418
00007FFC2F5AB3A1 |00007FFC2F5A9410
00007FFC2F5AB38C |00007FFC2F5A9408
0000000000000000 |00007FFC2F5A9400
00007FFC2F5AB384 |00007FFC2F5A93F8
0000000000000001 |00007FFC2F5A93F0*  ;Top of Stack (TOS)

Stack after two PUSHes...
00007FFC2F5AB38C |00007FFC2F5A9408
0000000000000000 |00007FFC2F5A9400
00007FFC2F5AB384 |00007FFC2F5A93F8
0000000000000001 |00007FFC2F5A93F0 ;Previous TOS
0000000000000045 |00007FFC2F5A93E8 ;Watch the stack 'grows' downwards towards lesser memory
0000000000000077 |00007FFC2F5A93E0* ;New TOS. The last item PUSHed

Stack after one POP...
00007FFC2F5AB3A1 |00007FFC2F5A9410
00007FFC2F5AB38C |00007FFC2F5A9408
0000000000000000 |00007FFC2F5A9400
00007FFC2F5AB384 |00007FFC2F5A93F8
0000000000000001 |00007FFC2F5A93F0
0000000000000045 |00007FFC2F5A93E8* ;New TOS after one item is POPped to RCX
;77h is no longer 'available' on the stack, but RCX now has it
;Stack 'shrinks' upwards the address, one 'notch'


This effect can be achieved by using "stackview" routine.

Code: [Select]
global _start

section .data
empty db 'The stack (assumed) empty...',0ah,0
two db 'Stack after two PUSHes...',0ah,0
one db 'Stack after one POP...',0ah,0
val1 dq 45h    ;will push these two data onto the stack
val2 dq 77h    ;and pop this one later

section .text
_start:
mov rax,empty
call prnstrz
mov rax,6 ;view 6 items of stack
call stackview
call prnline

push qword[val1] ;push a var
mov rax,[val2] ;just a variety. Push a reg
push rax

mov rax,two
call prnstrz
mov rax,6
call stackview
call prnline

pop rcx ;pop 1 item from TOS(*)

mov rax,one
call prnstrz
mov rax,6
call stackview

call exitx

But you probably don't understand a complicated example like the above. How about exploring it on your own, like this;

Code: [Select]
push 45h
push 77h
mov rax,5   ;see 5 items of stack (1 item = 8 bytes)
call stackview

and see what's the output. Then try popping one item and the print the stackview again. That easy ;D
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 04:19:05 PM
Or maybe you want to try exploring the FPU instruction set on your own...

Code: [Select]
global _start

section .data
pointX dq 5.61
pointY dq 3.45

section .text
_start:

finit ;clear FPU, just to be safe.
fld qword[pointX] ;load to ST1
fld qword[pointY] ;load to ST0
call fpu_stack ;See current stack
call prnline
fdiv st0,st1 ;divide st0 by st1, and save the result to ST0
call fpu_stack ;see the result in ST0

call exitx

And here's the quick and visual output for your next phase of learning ;D

Code: [Select]
ST0: 3.450000000000000177
ST1: 5.610000000000000319
ST2: ...
ST3: ...
ST4: ...
ST5: ...
ST6: ...
ST7: ...

ST0: 0.614973262032085558
ST1: 5.610000000000000319
ST2: ...
ST3: ...
ST4: ...
ST5: ...
ST6: ...
ST7: ...

Note the output is in full REAL10 format while your data is in REAL8 format, so you'll see trailing values from the FPU stack. It's expected and not an error.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 04:22:08 PM
Good luck and be register-safe!  ;D
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 14, 2016, 09:26:12 PM
Before I forgot, here's one very basic way to use "base6.dll" for windows

Code: [Select]
;Example of accessing "base6.dll" from thisfile.asm
;Compile: nasm -f win64 thisfile.asm
;Link   : golink /console /entry _start thisfile.obj base6.dll
;----------------------
global _start

extern _dumpreg
extern _prnstrz
extern _exitx

section .data
s db 'Hello World',0ah,0

section .text
_start:
        mov     rax,s
        call    _prnstrz
        push    0
        call    _dumpreg
        call    _exitx

Not much different from accessing other DLL files. You need a linker like GoLink btw.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 17, 2016, 06:53:37 PM
Added and update a few routines.

Revision 2.7 (Sept 18th, 2016)
Code: [Select]
[+] rand - get one unsigned random int of range 0 to N
[+] randq- Generate unique random integers (unsigned) of range 0 to N-1
           and save to an array
[-] rndigit - deleted
[+] int2str
[+] file_copy
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 25, 2016, 09:38:41 PM
I understand that it is a great pain to explain to beginners on basic things like converting an integer to string and anything similar. Without any helper tool (debugger is too much for a 'helper' at beginners level), explanations of things like registers, instructions and their effects can be very time-consuming. One such example is converting 1234 to string "1234" by using repetitive divisions of 10 by using DIV instruction and setup

Example of extracting every digit out of 1234 and convert each digit to their corresponding ASCII characters;
1234/10     = 123, modulo or digit 4  ==> add 0x30, we get char '4' or ASCII 0x34
123/10       = 12, modulo or digit 3  ==> add 0x30, we get char '3' or ASCII 0x33
12/10        = 1, modulo or digit 2 ==> add 0x30, we get char '2' or ASCII 0x32
1/10          = 0, modulo or digit 1 ==> add 0x30, we get char '1' or ASCII 0x31

Which can be done this way for one round digit pass, getting digits from the back
Code: [Select]
;'Confident' mode
xor edx,edx     ;cleared before DIV
mov eax,1234    ;the digit to convert
mov ecx,10
div ecx     ;answer in EAX, modulo in EDX (e,g 1234/10 ==> EAX = 123, EDX = 4)
add dl,0x30 ;convert digit in EDX to ASCII '4'

That's the easy part. The difficult part is the Explanation, and for some beginners, the really need some visual cues to answer their famous 'what-the-hell-is-the instructor-is-talking-about' question. Luckily that can be partially solved by using the routines like "dumpreg", "halt" and "prnchr" where codes like the above can be visually and interactively turned from 'eeww' format into an instructional material to create a more dynamic and interactive teaching and learning setting where helper routines can be inserted at any point of execution. This can greatly saves time and enhance effectiveness.

Code: [Select]
        ;'Instructional' mode
        mov     eax,1234;Value to convert
        mov     ecx,10  ;Divisor
        mov     edx,0   ;Clear before every DIV

        push    1       ;View all involved registers as unsigned int
        call    dumpreg ;watch ECX,EDX and EAX in initial state
        call    halt    ;Pause to observe

        div     ecx     ;Perform DIV. EAX=answer EDX=modulo (remainder)

        push    1       ;view dump as unsigned int
        call    dumpreg ;See changes in EDX and EAX. EDX = 4
        call    halt    ;pause to observe

        add     dl,30h  ;convert digit 4 to ASCII character '4'
        push    0       ;Now view in HEX format
        call    dumpreg ;EDX now becomes ASCII '4'(0x34) no more digit 4
        call    halt

Producing this progressive and interactive output, by using the  "halt" and "dumpreg" combo to explain the code behavior at almost every point of instructions.

First dumpreg output:
Code: [Select]
EAX|0000001234 EBX|0002781184 ECX|0000000010
EDX|0000000000 ESI|0004198400 EDI|0004198400
EBP|0000917396 ESP|0000917380 EIP|0004198415

Second dumpreg output:
Code: [Select]
EAX|0000000123 EBX|0002781184 ECX|0000000010
EDX|0000000004 ESI|0004198400 EDI|0004198400
EBP|0000917396 ESP|0000917380 EIP|0004198429

Last dumpreg output
Code: [Select]
EAX|0000007B EBX|002A7000 ECX|0000000A
EDX|00000034 ESI|00401000 EDI|00401000
EBP|000DFF94 ESP|000DFF84 EIP|0040102C

After a user is satisfied that instructions are all in order and behave as they are intended, the routines can be deleted and the beginners can put their code back to 'confident' mode. LOL ;D
 
Title: Re: General Purpose Lib for Extreme Beginners
Post by: Frank Kotler on September 25, 2016, 10:04:27 PM
Great explaination, stressful! The only thing I would stress is the necessity of zeroing edx before the "div" every time!

Best,
Frank

Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 25, 2016, 11:31:13 PM
Yeah I know that Frank but thanks for the reminder. Just wanted to avoid the most common beginners mistakes in such a short explanation. Stay cool Frank. You're my favorite teacher!   
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 26, 2016, 12:01:15 AM
If we ignore the string buffer, we can build a simpler loop from the program above and see all the extracted digits via "dumpreg"

Code: [Select]
            mov eax,1234
            mov ecx,10
.getDigit:
            mov edx,0   ;must be cleared
            div ecx       ;Answer in EAX. Digit in EDX
            add dl,0x30   ;the ASCII char we need. Converted from digit to ASCII.
            push 1          ;push 0 to view in Hex
            call dumpreg
            call halt
            test eax,eax   ;as long as EAX not 0, get next digit
            jnz .getDigit

or perhaps you're done with the dumpreg and wanted to see some real screen output... well why not? If C can do that, ASM can too! But until we get to the buffer/stack processing, we should settle on printing it in reverse order! C can't do this that easy! Hehehe  ;D

Code: [Select]
    global _start

section .text
_start:
    mov eax,1234
    mov ecx,10
.getDigit:
    xor edx,edx
    div ecx
    add dl,0x30
    xchg edx,eax   ;copy edx to eax for prnchr routine
    call prnchr      ;proof that DL is an ascii char. Print the ASCII char in EAX.
    xchg edx,eax   ;restore their previous values
    test eax,eax    ;if EAX <> 0, loop some more. EAX = loop sentinel
    jnz .getDigit
                         ;We reach here if EAX is indeed 0
    call exitx         ;exit the program

But seriously, string processing is not yet necessary because we just want to have a proof-of-concept program - 1) how to split an integer into digits and 2) turn each digit to their ASCII equivalences. That's all that matters right now. There's still more to go (signed and unsigned, overflow, hex, octal etc) but that should do it for now.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 26, 2016, 12:57:42 AM
Btw, I can't put enough emphasis on the importance of "dumpreg" routine. You need to get yourself familiar with it, and how to properly call it because it's going to be your best buddy. Here's an example of calling the "dumpreg" in 3 different format, displaying the same values;

Code: [Select]
mov eax,-3 ;test value

push 0 ;Display as unsigned Hex
call dumpreg
call prnline

push 1 ;Option to display as unsigned Int
call dumpreg
call prnline

push 2 ;Option to display as signed integer
call dumpreg
call prnline

The output. Same values, different format;

Code: [Select]
EAX|FFFFFFFD EBX|7FFD4000 ECX|00000000
EDX|00401000 ESI|00000000 EDI|00000000
EBP|0006FF94 ESP|0006FF8C EIP|00401005

EAX|4294967293 EBX|2147303424 ECX|0000000000
EDX|0004198400 ESI|0000000000 EDI|0000000000
EBP|0000458644 ESP|0000458636 EIP|0004198417

EAX|-0000000003 EBX|+2147303424 ECX|+0000000000
EDX|+0004198400 ESI|+0000000000 EDI|+0000000000
EBP|+0000458644 ESP|+0000458636 EIP|+0004198429

Read the header description of "dumpreg".

Enjoy your coding.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on September 28, 2016, 09:13:33 AM
[Continue]

The above process is for converting decimal integer to decimal string (1234 to "1234"). The process is exactly the same for octal string, except the divisor is 8. (Valid octal digits are from 0 to 7).

For hex, the divisor is 16 (valid hex digits are from 0,1,2...9,A,B,C,D,E,F). The process is a bit different. Say we have 0x3CCF2

0x3CCF2 / 16 ==> EAX = 0x3CCF, EDX = 2  ==> add 0x30 you get ASCII char '2'
0x3CCF / 16 ==> EAX = 0x3CC, EDX = F  ==> if value is over 9, add 0x37. Else just add 0x30. You get ASCII char 'F'

Code: [Select]
       mov eax,0x3CCF2
       mov ecx,16
getmore:
       xor edx,edx
       div ecx
       add dl,0x30  ;add to everyone
       cmp dl,'9'   ;if ASCII '9' and below
       jbe normal
       add dl,7     ;if alpha hex (A to F)
normal:
       push 0
       call dumpreg
       call halt
       cmp eax,0
       jne getmore

With this series of "dumpreg" output

Code: [Select]
EAX|00003CCF EBX|00323000 ECX|00000010
EDX|00000032 ESI|00401000 EDI|00401000  ;EDX=ASCII '2'
EBP|000DFF94 ESP|000DFF84 EIP|00401019

EAX|000003CC EBX|00323000 ECX|00000010
EDX|00000046 ESI|00401000 EDI|00401000  ;EDX=ASCII 'F'
EBP|000DFF94 ESP|000DFF84 EIP|00401019

EAX|0000003C EBX|00323000 ECX|00000010
EDX|00000043 ESI|00401000 EDI|00401000  ;EDX=ASCII 'C'
EBP|000DFF94 ESP|000DFF84 EIP|00401019

EAX|00000003 EBX|00323000 ECX|00000010
EDX|00000043 ESI|00401000 EDI|00401000  ;EDX=ASCII 'C'
EBP|000DFF94 ESP|000DFF84 EIP|00401019

EAX|00000000 EBX|00323000 ECX|00000010
EDX|00000033 ESI|00401000 EDI|00401000  ;EDX=ASCII '3'
EBP|000DFF94 ESP|000DFF84 EIP|00401019

Next time.
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on October 04, 2016, 12:02:49 PM
I added full set of Win source so now both Linux and Win sources are on equal terms.

I changed the name of routine "exit" to "exitx" to avoid name clashes with API. Others are not affected.

Enjoy it.

Oct 4th, 2016
Title: Re: General Purpose Lib for Extreme Beginners
Post by: debs3759 on October 05, 2016, 07:14:54 PM
Thanks. I've downloaded it and will take a look at the code when I finish researching all the new CPUs from the last decade for my updated CPUID code :)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: gharald on October 18, 2016, 06:20:02 PM
Thanks a lot Sandakan, very useful code !
And very instructive too for beginners !
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on November 03, 2016, 02:23:42 PM
Thanks gharald and debs.

Although this library is not specifically designed for external access, the stack versions sbase3, either in DLL or .o formats can be conveniently accessed from within a 32-bit C program by observing these rules;

1) a routine with no return value is a "void" type. If it returns an int, it's an "int" type and so on.
2) a routine with arguments should be called with relevant and correct arguments (types and number of arguments)
3. For win32 source, you need to tag the routines names with a leading underscore (_) as shown in the code below. Don't do that for Linux source.

Below is an example on how you can view the stack frame and the current register dumps from C functions. This way, you can have an in-depth look to what's really going on with the the registers and the stack as it goes along the execution path.

Code: [Select]
/***************************************************
 Example: Calling sbase3 (stack version) from C
 Win32 : gcc -m32 this.c sbase3.dll -o this.exe
 Linux32: gcc -m32 this.c sbase3.o -o this
 On 32-bit systems, no need for -m32 switch
***************************************************/
#include <stdio.h>

extern void _dumpreg(int);
extern void _stackview(int);
int testfunc(int, int, char);

int main()
{
int x=10;

x = testfunc(3,2,'A');
//printf("x = %d\n",x);
_dumpreg(0);
}
int testfunc(int a, int b, char c)
{
int x=-2,y=-1;
_stackview(14);  //increase to view more

return b*2;  //EAX will capture this.
}

Yielding this output

Code: [Select]
00000041 |0061FF18  ;argument 3 = 'A'
00000002 |0061FF14  ;argument 2 = 2
00000003 |0061FF10  ;argument 1 = 3
00401412 |0061FF0C  ;EIP. Return address
0061FF38 |0061FF08  ;Caller's EBP
B7D13186 |0061FF04  ;C's thingies
741DD250 |0061FF00
FFFFFFFE |0061FEFC  ;local. x = -2
FFFFFFFF |0061FEF8  ;local. y = -1
0061FEE0 |0061FEF4  ;C thingies
00401C70 |0061FEF0
00000041 |0061FEEC  ;what is C doing here? This is the third argument re-appearing.
004012A0 |0061FEE8  ;C push ESI/EDI. Nobody knows why
004012A0 |0061FEE4*  ;C push ESI/EDI
EAX|00000004 EBX|00297000 ECX|00000001   ;EAX = return value. X will capture it
EDX|00000000 ESI|004012A0 EDI|004012A0
EBP|0061FF38 ESP|0061FF14 EIP|0040141A

You can test calling other routines from within C. But not all routines are suitable for such purposes and I haven't thoroughly tested them all. This is for "sbase3" only. Other binaries are not suitable for such purposes (due to different calling conventions etc).
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on November 09, 2016, 05:42:40 AM
-deleted-
Title: Re: General Purpose Lib for Extreme Beginners
Post by: stressful on November 09, 2016, 05:43:45 AM
-deleted-
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on November 20, 2016, 08:03:39 PM
Due to board's restrictions (I think), I can no longer update the main attachment in the first post.
As an alternative, the latest revision can be downloaded here: BASELIB (https://plus.google.com/collection/EGwClB)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on December 02, 2016, 04:38:52 PM
I uploaded the latest version (Revision 1.0.18) to my Google+ page. You can refer to the:
BASELIB (https://plus.google.com/collection/EGwClB) Collection section or visit 
the profile (https://plus.google.com/108856344528527240894) for other things related to the library.

You can safely ignore all other attachments you might found on this board since I can't no longer update them due to board restrictions. But I'll post future updates announcement here.
 
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: Mich on December 03, 2016, 03:26:04 AM
Windows 32 bit
This is my environment
Trying out your lib
Quote
[SECTION .drectve info align=8]
DB "kernel32.dll", 0x20 ; Space Terminated Strings
DB "MSVCRT.dll", 0x20
;DB "sbase3.dll",0x20

%include "sbase32w.asm"

extern ExitProcess
section .data
hello db 'Hello World',0
fmt db"%c",0

section .code
global main

main:
 call readch      ;this will crash
 call exitx
 
;   call getchar
;   push eax
;   push fmt
;   call printf
;   add   esp,8
;   push 0
;   call ExitProcess

;\nasm\bin\NASM -f win32
;\nasm\bin\golink /console /entry main

This compiles but crashes
The quoted code works as I understand.
Mich
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on December 03, 2016, 05:25:28 AM
Hi Mich. I don't quite understand your current code setup. But for calling readch in an interactive session, you probably want to halt the screen after that.

Code: [Select]
call readch
call halt
;... the key in AL
call exitx

Similar example can be seen in my post #3 on the first page.

Regards

Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on December 11, 2016, 10:43:26 PM
I uploaded the latest version (Revision 1.1.0) to my Google+ page. You can refer to the:

BASELIB (https://plus.google.com/collection/EGwClB) Collection section or visit 
the profile (https://plus.google.com/108856344528527240894) for other things related to the library.

You can safely ignore all other attachments you might found on this board since I can't no longer update them due to board restrictions. But just in case, I am attaching the latest release here in this post (see attachment).

I don't have the plan to extend the support for BASELIB any further. If there's no more fatal bug, then take this as the final update.

Regards
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: Mich on December 12, 2016, 03:48:54 AM
I'm just a computer enthusiast and have no formal training in computer science or programming. I'm just an old guy trying to beat Alzheimer decease by trying to grasp something.

Using your lib I compiled and run the following proggie on Vista 32bit:
Code: [Select]
;--------------------------------------------
; Compile: nasm -f win32 sbase32w.asm
; Link:
; golink /console /entry main sbase32w.obj kernel32.dll msvcrt.dll
;--------------------------------------------

Extern ExitProcess
section .bss
CharFromGetChar resb 1

section .data
hello db 'Hello World',0xa,0

section .text
global Start
Start:
push hello
call prnstrz

call readch
mov [CharFromGetChar],al
push CharFromGetChar
call prnchar
call exit
;############################################
19456bytes

Quote
CPU Disasm
Address   Hex dump          Command                                  Comments
<ModuleEn /.  68 00604000   PUSH OFFSET 00406000                     ; [???
00401005  |.  E8 EE400000   CALL 004050F8         ;call prnstrz
0040100A  |.  E8 51410000   CALL 00405160                            ; /Arg1 = sbase32w.405160
0040100F  |.  A2 10604000   MOV BYTE PTR DS:[406010],AL              ; \???
00401014  |.  68 10604000   PUSH OFFSET 00406010                     ; [???
00401019  |.  E8 763F0000   CALL 00404F94                            ; [sbase32w.00404F94
0040101E  |.  E8 31600000   CALL <JMP.&msvcrt.exit>

Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
004050F8  |?  55            PUSH EBP
004050F9  |?  89E5          MOV EBP,ESP
004050FB  |?  83EC 60       SUB ESP,60
004050FE  |?  83E4 F0       AND ESP,FFFFFFF0
00405101  |?  660F7F0424    MOVDQA DQWORD PTR SS:[ESP],XMM0
00405106  |?  660F7F4C24 10 MOVDQA DQWORD PTR SS:[ESP+10],XMM1
0040510C  |?  660F7F5424 20 MOVDQA DQWORD PTR SS:[ESP+20],XMM2
00405112  |?  660F7F5C24 30 MOVDQA DQWORD PTR SS:[ESP+30],XMM3
00405118  |?  660F7F6424 40 MOVDQA DQWORD PTR SS:[ESP+40],XMM4
0040511E  |?  660F7F6C24 50 MOVDQA DQWORD PTR SS:[ESP+50],XMM5
00405124  |?  50            PUSH EAX
00405125  |?  51            PUSH ECX
00405126  |.  52            PUSH EDX
00405127  |?  FF75 08       PUSH DWORD PTR SS:[EBP+8]
0040512A  |?  E8 191F0000   CALL <JMP.&msvcrt.printf>
0040512F  |.  83C4 04       ADD ESP,4
00405132  |?  5A            POP EDX
00405133  |?  59            POP ECX
00405134  |?  58            POP EAX
00405135  |?  660F6F0424    MOVDQA XMM0,DQWORD PTR SS:[ESP]
0040513A  |?  660F6F4C24 10 MOVDQA XMM1,DQWORD PTR SS:[ESP+10]
00405140  |?  660F6F5424 20 MOVDQA XMM2,DQWORD PTR SS:[ESP+20]
00405146  |?  660F6F5C24 30 MOVDQA XMM3,DQWORD PTR SS:[ESP+30]
0040514C  |?  660F6F6424 40 MOVDQA XMM4,DQWORD PTR SS:[ESP+40]
00405152  |?  660F6F6C24 50 MOVDQA XMM5,DQWORD PTR SS:[ESP+50]
00405158   ?  89EC          MOV ESP,EBP
0040515A      5D            POP EBP
0040515B      C2 0400       RETN 4

The following code produces the same output by calling msvcrt functions directly:
Code: [Select]
section .data
hello db 'Hello World',0xa,0
extern printf,putchar, getchar,exit

section .text
global Start
Start:
push hello
call printf
add esp,4
    call getchar
and eax,0xff
push eax
call putchar
add esp, 4
call exit
2048bytes

I listed only the prnstrz in the disassembly but similar code is for many of the c funcs.

I do not quite see why not just calling the functions directly? What documentation could you refer me to?
Klod
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on December 12, 2016, 04:48:56 AM
Mich,

Windows printf, getch, putchar - they all destroy xmm0-xmm5.

BASELIB is a modular library. As with any other library, the design of this library have to include and consider many other things when wrapping API functions. This is especially true in BASELIB because it contains many SSE instructions and routines that use XMM registers. If I don't save the XMM registers, then beginners may have to save the XMMs manually every time they want to use prnstrz when making SSE-related programs.

I have no documentation, but since you already know how to use the debugger, why don't you debug the printf function and see what registers got destroyed in there.

This is Windows API design issue. You don't have this issue in Linux. Hope this helps.
 
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on December 23, 2016, 08:34:41 PM
I am closing BASELIB with this final release (see attachment) before 2017 approaches by adding 16-bit COM source to the library. Have long abandoned it but I don't see why it should sit there in my harddrive collecting dusts and rusts. See if you can make good use of it.

You can also download BASELIB from my page (https://plus.google.com/108856344528527240894) (see BASELIB: COLLECTION section)

or just in case Google+ is scrapped by google, you can download it directly from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

Updates
13.Jan.2017
.small enhancement to stackview
.small enhancement to flags
.Corrrected header entry for prnxmm (64-bit only)
.Added scan_byte, scan_word, scan_dword, scan_qword
.Modify dumpreg output

== Attachment deleted ==
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on January 25, 2017, 10:42:59 PM
26.Jan.2017 Update 

.Fixed a small but fatal bug in "base64w.asm". Others are not affected
.Added .SO binary versions of BASELIB (for Linux only)

You can visit my profile for similar downloads and other things here (https://plus.google.com/108856344528527240894)

or you can directly download them from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

 
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on February 14, 2017, 01:51:17 AM
14.Feb.2017 

.Released BASE2.0 - All-binary (.so,.obj,.o,.dll,.lib). No source. Not quite compatible with BASELIB.

You can visit my profile for similar downloads and other things here (https://plus.google.com/108856344528527240894)

or you can directly download them from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://plus.google.com/108856344528527240894)

or you can directly download them from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://plus.google.com/u/0/108856344528527240894)

or you can directly download them from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

You can safely ignore other attachments found previously in this thread.
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://plus.google.com/108856344528527240894)

- or you can directly download them from my drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

or from sourceforge (https://sourceforge.net/projects/baselibs/files/)

Please ignore other attachments of similar libraries found in the previous posts.
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://sourceforge.net/projects/baselibs/files/)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://sourceforge.net/projects/baselibs/files/). Or download directly below (revision 2.7)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://sourceforge.net/projects/baselibs/files/) or from my Google+ (https://plus.google.com/u/0/108856344528527240894) profile.

You should ignore older attachments found in this thread or in other thread.
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://sourceforge.net/projects/baselibs/files/)
2. my Google drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg) or
3. my Google+ profile (https://plus.google.com/108856344528527240894).

You should ignore older attachments found in this thread or in other thread..
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful 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 (https://sourceforge.net/projects/baselibs/files/) 
2) Google Drive's BASEUPLOADS (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on August 19, 2018, 05:16:49 PM
(13.Oct.2018) Revision 4.0.3
[1] Minor improvement / fixes here and there. Nothing big.

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 (https://sourceforge.net/projects/baselibs/files/) 
2) Google Drive's BASEUPLOADS (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

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

Regards
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on September 22, 2018, 11:14:17 AM
== deleted ==
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on September 22, 2018, 11:33:28 AM
== deleted ==
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on September 23, 2018, 03:59:33 AM
== deleted ==
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on September 23, 2018, 04:47:29 PM
== deleted ==
 
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: GabrielRavier on October 02, 2018, 07:44:55 AM
I like optimizing stuff, so I thought I could give some "better" versions of the functions given here :

rndint : https://pastebin.com/5LqfueCk
opsize : https://pastebin.com/hfYpGFq6
iseven : https://pastebin.com/ALDzw7Ey
isodd : https://pastebin.com/ebz2S8Fj
chr_islower : https://pastebin.com/NsSLinge
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on October 02, 2018, 08:44:30 AM
Excellent stuff, Gabriel! Keep it coming.

Thank you.

p/s I am taking notes on contributors.
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on October 02, 2018, 09:01:15 AM
Btw, your modification to opsize is a bit off, especially if it has 0 size. But I got your point.

Code: [Select]
    lbl1:
          ;nothing
    lbl2:

    ...
    push lbl1
    push lbl2
    call opsize
    call dumpreg  ;EAX returns incorrect value

Hope to see more from you.
 
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: GabrielRavier on October 03, 2018, 04:34:02 PM
Oops, sorry for the error

I also wanted to submit a new function, which checks for cpuid being available : https://pastebin.com/fR4knK95
64-bit version : https://pastebin.com/N5mxRwYe
Title: Re: BASELIB: General Purpose Lib for Beginners
Post by: stressful on October 17, 2018, 06:34:48 PM
I am migrating my BASELIB/CPULIB project to Sourceforge now as the files are getting larger in size. You can download the updates and other goodies from either one of these;

1. Sourceforge baselibs (https://sourceforge.net/projects/baselibs/files/) project. Browse the files
2. Directly from my Google Drive (https://drive.google.com/drive/folders/0Bxucel0PYpwyc3pydHZlR051ajg)

Good luck and enjoy your coding.