Author Topic: NASM Learners Library for 32-bit and 64-bit Linux  (Read 33008 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
NASM Learners Library for 32-bit and 64-bit Linux
« on: March 01, 2015, 06:03:05 AM »
This is a library for 64-bit Linux intended for learners, newbies and any party interested in learning x86_64 assembly language. This library contains various basic routines that provide new learners with a set of enabling and learning tools. This is a basic learning library where transparency and readability is the main objective, rather than optimizations or performance.

The attachment contains 3 files: nsm64.asm, lib64.o and nobjlist.inc and nsm32.asm for 32-bit version. nsm64.asm is the main file while others are optional. lib64.o is the object equivalence to nsm64.asm. It is in non-readable, non-executable object format. nobjlist.inc is required if learners want make their own object file out of the main source file (nsm64.asm). Make sure to backup the original files.

The routines are register-based routines. Arguments are fed into RDI, RSI, RDX, R8. For floating point argument, parameter is fed into XMM0. Return values are placed in either RAX (for integer) or XMM0 (for real). Only "prtch" and "flags" routines have their own different paramater-passing methods. All affected registers are preserved by the callees so that new learners can reduce mistakes and errors at the earliest learning phase. Stack unwinding is done by the callees.

For 32-bit version, arguments are passed to the stack. The first argument is the last one pushed onto the stack.

I tried my best to make the library an ABI-Compliant. That means these routines can also be called from C. It can also be used in its object form and be called from elsewhere (lib64.o).

This library also contains various basic floating-point routines so that new learners do not have to limit themselves to integer-only assembly programming and be less dependant on C library. This promotes more natural assembly learning environment without heavy reliance on third party library.

Each routine has its own descriptions located at their headers. Each describes what a routine does, the arguments required and the return value (if any). I hope learners can get used to the arguments and their types as soon as possible before using the routines.

For learning sake, I intentionally left all the comments blank so that learners should always come back to this forum and participate in more productive discussions and learning activities. 

If you found any bug or have suggestions for improvement feel free to contact me. I may also introduce more updates to the original file if time permits.

Happy Coding.

soffianabdulrasad @ gmail . com

EDIT: A new attachment (NASMLIB) contains both nsm64 and nsm32 for single download.

List of routines for 64-bit library (as of April 27th 2015);

Code: [Select]
prtreg    
dumpreg    
flags    
prtdec
prtdecu    
prthex    
prtoct    
prtbinf
prtbin    
fpbin    
prtdbl
prtflt    
prtstr  
prtstrz  
prtchar  
prtchr
getch  
getchr  
getstr  
getstrz  
getint  
getdbl
getflt  
fpu_stack  
fpu_sflag  
fpu_cflag  
fpu_reg  
fpu_copy
str2int
str2dbl
bconv  
bitfield  
powb10
pow
sqroot  
rad2deg  
deg2rad  
sine  
tangent  
cosine
atangent  
prtxmm
dumpxmm
dumpfxmm
prtxmmq
prtxmmf
sse_flags
rndigit
chr_find
chr_count  
str_copy  
str_length
str_cmps
str_cmpz
str_reverse
str_alternate
str_tolower
str_toupper
newline
halt
opsize
exitp
exit

One sample code demonstrating various routines of the nsm64;

Code: [Select]
global _start

section .data
s dq 0xff3e
x dq -34.56
hello db 'Hello World!',0

section .text
_start:
mov rdi,hello
call prtstrz

call newline

mov rdi,[s]  ;first argument in RDI
call prtdec  ;Display signed integer

call newline

        ;call    getdbl
movq    xmm0,[x]  ;first argument in xmm0
call prtdbl
call exit

Producing this output:
Code: [Select]
Hello World!
65342
-34.56000000000000
« Last Edit: April 29, 2015, 07:22:43 PM by stressful »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM Learners Library for 64-bit Linux
« Reply #1 on: March 01, 2015, 08:59:02 PM »
That looks really nice! It may even inspire me to get a 64-bit machine running again so I can try it out. I will almost certainly have comments/questions as I learn from it. Thanks, Soffian!

Best,
Frank


Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 64-bit Linux
« Reply #2 on: March 01, 2015, 09:11:17 PM »
It's nothing much Frank. Just some basic routines that I think can help newbies to go through those 'difficult' time. Rather than starting from blank, some basic learning tools should be made available to them to get them going. Nothing fancy though. I am not a good programmer to be honest. Still have lots to learn myself.

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 64-bit Linux
« Reply #3 on: March 01, 2015, 09:19:39 PM »
Although this basic library doesn't use any C routines, they can be called directly from C via its "lib64.o" object file. A demo C program below shows (Note the missing stdio.h statement);

Code: [Select]
//gcc fromc.c lib64.o -o fromc
extern int chr_count(char *,char);
extern double getdbl();
extern void prtdbl(double);
extern void newline();
extern void prtstrz(char *);
extern void prtdec(int);

int main()
{
double x=0.0;
int count=0;
char *y="Enter a double: ";
char *z="You entered   : ";

prtstrz(y);
x=getdbl();
prtstrz(z);
prtdbl(x);
newline();
count=chr_count(y,'e'); //Count 'e' from the string y
prtdec(count);
newline();
return 0;
}

This could be a bit advance, but it demonstrates some basic linking process and how it is important to match the arguments and the return values in accordance to the ABI.
« Last Edit: April 29, 2015, 07:23:40 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 64-bit Linux
« Reply #4 on: March 05, 2015, 11:32:03 AM »
This one is for 32-bit linux version library. Almost identical in functionality with the 64-bit version but this one uses stack-based argument passing. Arguments are pushed onto the stack in the particular order (read header descriptions of each). Share, learn and love :D

[Attachment deleted. See Post #1 for download]
 
« Last Edit: April 27, 2015, 01:04:36 AM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 64-bit Linux
« Reply #5 on: March 05, 2015, 08:25:36 PM »
At times you may want to use floating point literal in your program. To do that, you need to find its hex equivalence. This is how prthex routine can be useful as converter.

Code: [Select]
global _start

section .data
x dq 567.855

section .text
_start:
mov rdi,[x] ;4081BED70A3D70A4
call prthex
call exit

Now u can use the value displayed as a FP representation in hex format. No need for C or other macro. Saves you lots of time and effort. 

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 64-bit Linux
« Reply #6 on: March 05, 2015, 08:40:45 PM »
To access the library via static linking, use the accompanying lib64.o file. One example;

Code: [Select]
;Compile: nasm -f elf64 test.asm
;Link : ld test.o lib64.o -o test
section .data
hello db 'Hello World!',0ah,0

section .code
global _start
extern prtstrz
extern exit

_start:
mov rdi,hello
call prtstrz    ;display 0-ended string
call exit

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #7 on: April 27, 2015, 12:59:51 AM »
I fixed some embarrassing bugs in prtdbl and prtflt when dealing with 0.0 and -0.0. Sorry  ;D

I added 4 new routines dealing mostly with XMM registers;

1) dumpfxmm - view XMM dump in floating format instead of just hex (as in dumpxmm)
2) prtxmmf - view an XMM register as 4 floats
3) prtxmmq - view an XMM register as 2 doubles
4) opsize - Non-XMM routine. To calculate code or data size between 2 labels. Example;

Code: [Select]
a:
nop
nop
b:

mov rdi,a   ;order is not important
mov rsi,b
call opsize  ;should produce size of 2

Will update more if I got some more spare time.

Cheers  ;D

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #8 on: April 27, 2015, 02:31:20 AM »
Here i wrote a small conversion program that enables learners to perform a quick base conversion from popular bases (hex, bin, decimal, octal) to another bases up to base 36 (max). This is a command-line program (not part of the library above). This program below also demonstrates basic steps in creating command-line programs.

Sample run;

Code: [Select]
$ ./baseconv 45h 10  --> convert 45 hex to base 10
$ ./baseconv 10111000111b --> convert 10111000111b binary to base 8 (octal)
$ ./baseconv 26544437654o 30 --> convert one octal number to base 30
$ ./baseconv --> Display help message.

The program;

Code: [Select]
;Compile: nasm -f elf64 baseconv.asm
;Link : ld baseconv.o -o baseconv
;Run :./baseconv 3ccdeh 5. Convert a hex to base 5

global _start

section .data
errmsg db 'baseconv    : 64-bit base converter (integer)',0ah
  db 'usage : baseconv <value+suffix> <base>',0ah
  db 'example : baseconv 35h 10 - convert 35h to decimal',0ah
db '      : <base> maximum is 36',0ah
db '      : <suffix> - h,d,o,b',0ah
db 'author : soffianabdulrasad @ gmail . com',0ah,0

section .bss
numb    resq 1
base resq 1
tmp resb 1

section .code
_start:
mov rbp,rsp
mov rbx,[rbp] ;Number of arguments in RBX
cmp rbx,3 ;If less arguments...
jne .err ;  ...show help message
mov rsi,[rbp+16] ;First argument
call atoi ;Convert to integer
mov [numb],rax     ;return value in RAX
mov rsi,[rbp+24] ;Second argument
call atoi ;Convert to integer
;--------------------------
.bconv:
mov rbx,rax
mov rax,[numb]
xor rdi,rdi
test rax,rax
jns .start
neg rax
push rax
mov al,'-'
call  ch_out
pop rax
.start:
xor rdx,rdx
div rbx
push rdx
test rax,rax
jz .fine
inc rdi
jmp .start
.fine:
pop rax
add al,30h
cmp al,39h
jbe .num
add al,7
.num:
call ch_out
dec rdi
jns .fine
mov al,0ah
call ch_out
jmp .done
;--------------------
.err:
mov rdi,errmsg
mov rcx,-1
xor al,al
repne    scasb
mov rdx,-2
sub rdx,rcx
mov edi,1
mov eax,edi
mov rsi,errmsg
syscall
;-------------------
.done:
xor edi,edi
mov eax,60
syscall
ret
;--------------------
atoi:
xor r15,r15
xor rax,rax
xor r8,r8 
lodsb
cmp al,'-'
jne .norm
mov r15b,1
.begin:
lodsb
.norm:
test al,al
jz .select
push rax
inc r8
jmp .begin
.select:
pop rax
cmp al,'h'
je .hexadecimal
cmp al,'H'
je .hexadecimal
cmp al,'b'
je .binary
cmp al,'B'
je .binary
cmp al,'o'
je .octal
cmp al,'O'
je .octal
cmp al,'d'
je .decimal
cmp al,'D'
je .decimal
push rax
inc r8
.decimal:   
xor r9,r9
pop rax
sub rax,30h
add r9,rax
mov ecx,10
mov ebx,ecx
dec r8
jmp .translate
.hexadecimal:
xor r9,r9
pop rax
cmp al,'a'
jae .smalls
cmp al,'A'
jae .bigs
jmp .proceed
.bigs:
cmp al,'F'
ja .big
.big:
sub rax,7h
jmp .proceed
.smalls:
cmp al,'f'
ja .proceed
.small:
sub rax,27h
.proceed:
sub rax,30h
add r9,rax
mov ecx,16
mov ebx,ecx
dec r8
jmp .translate
.octal:
xor r9,r9
pop rax
sub rax,30h
add r9,rax
mov ecx,8
mov ebx,ecx
dec r8
jmp .translate
.binary:   
xor r9,r9
pop rax
sub rax,30h
add r9,rax
mov ecx,2
mov ebx,ecx
dec r8
jmp .translate
.translate:
dec r8
jz .exit
pop rax
cmp rbx,16
jne .proceed1
cmp al,'a'
jae .Smalls
cmp al,'A'
jae .Bigs
jmp .proceed1
.Bigs:
cmp al,'F'
ja .Big
.Big:
sub rax,7h
jmp .proceed1
.Smalls:
cmp al,'f'
ja .proceed1
.Small:
sub rax,27h
.proceed1:
sub rax,30h
mul rcx
add r9,rax
mov rax,rbx
mul rcx
mov rcx,rax
jmp .translate
.exit:     
cmp r15b,1
jne .out
neg r9
.out:
mov rax,r9
ret
;--------------------
ch_out:
push rax
push rdi
mov byte[tmp],al
mov rsi,tmp
mov edx,1
mov edi,edx
mov eax,edx
syscall
pop rdi
pop rax
ret

The downloads, plus the executable.

Hope you'll find it helpful and useful.
« Last Edit: April 27, 2015, 02:34:35 AM by stressful »

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #9 on: February 17, 2016, 11:11:05 AM »
My 1st post. So please understand my lack of knowledge.
So, how do I get the nsm64.asm  or nasmlib??????
I see nothing in your post on how to get them

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #10 on: February 17, 2016, 11:29:06 AM »
Both the 32- and 64-bit versions are zipped as an attachment to the first post in this topic. Post again if you have trouble with it.

Best,
Frank


Offline JD

  • New Member
  • Posts: 1
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #11 on: April 05, 2016, 02:58:22 PM »
Thank you for the hard work stressful. We are lucky to have you.  :D

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #12 on: May 05, 2016, 07:58:17 PM »
oh.. okay...

in a moment.

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #13 on: May 05, 2016, 08:40:52 PM »
One year later... LOL. Now I can't even remember how to edit my own post. So this is on a new post  :P

This is the latest source for nsm32 and nsm64. Both in source format. Native, low and easy for beginners to follow through and use. These are slightly better and offer more features. Non-ABI compliant.

Code: [Select]
prnchr
prnchrs
prnchar
prnstr        
prnstrz
prnstrd
readch
readchr
readstr
delay
mem_alloc      
mem_free    
file_new
file_open
file_read
file_write
file_close
file_size
exit    
exitp      
newline
halt    
prnstreg    
prnreg        
prnregd
prnregdu    
dumpreg
dumpregd    
dumpregdu      
dumpseg
dumpenv
flags      
stackview      
memview
memviewc
mem_reset
mem_set
mem_copy    
mem_load
opsize
opcode        
prnint        
prnintu
prnhex        
prnhexu
prnoct        
prnoctu
prnbin        
prnbinu
prnbinf
prnbinb
fpbin      
fpbind        
prndbl        
prndble
prndblr
prnflt        
prnfltr
prndblx
dblsplit    
fpdinfo
fpfinfo
dec2str
dec2stru    
hex2str
hex2stru    
dbl2str
readint
readflt
readdbl
fpu_stack      
fpu_sflag      
fpu_cflag      
fpu_tag
fpu_reg
fpu_copy    
fpu_precision      
fpu_round      
sse_round      
sse_flags      
prnxmm        
dumpxmm
clearxmm
prnymm        
dumpymm
clearymm    
prnintd
prnintw
prnintb
str2int
str2dbl
str2flt
dbl2int
int2dbl
rndigit
rndint        
isint      
digiti
digith
factorial
powint        
pow2    
iseven        
isodd      
bconv      
bitfield    
addf    
subf    
mulf    
divf    
pow
sqroot        
log10      
ln10    
deg2rad
rad2deg
sine    
tangent
cosine        
sincos        
atangent    
chr_isdigit
chr_isalpha
chr_isupper
chr_islower
chr_toupper
chr_tolower
chr_chcase        
chr_find    
chr_count      
chr_shuffle
ascii      
str_copy    
str_length        
str_cmpz    
str_cmp
str_toupper
str_tolower
str_reverse
str_trim    
str_wordcnt
str_token      
str_find    
str_findz
str_appendz
str_append
sort_byte
sort_int    
sort_dbl    
sort_dblx      
sort_flt    
aprndbl
aprndblx    
aprnflt
aprnint

The attachment (Update May 6th, 2016):



Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: NASM Learners Library for 32-bit and 64-bit Linux
« Reply #14 on: July 21, 2016, 04:24:56 PM »
August 14th, 2016 Update:

.Made some minor improvement to the library and added 7 more routines.
.Now the library has two different formats. One is in source format and the other is in static object format for your convenience.
.Included some new documentations in both folders for easy reference.

Since I can't retract/update the older uploads/attachments like the above, you should always download the latest version like this one and ignore the older ones.

This is final revision. Enjoy your learning of assembly programming.

Code: [Select]
;List for 64-bit routines
prnchr
prnchrs
prnchar
prnstr        
prnstrz
prnstrd
readch
readchr
readstr
delay
mem_alloc      
mem_free    
file_new
file_open
file_read
file_write
file_close
file_size
exit    
exitp      
newline
halt    
prnstreg    
prnreg        
prnregd
prnregdu    
dumpreg
dumpregd    
dumpregdu      
dumpseg
dumpenv
flags      
stackview      
memview
memviewc
mem_reset
mem_set
mem_copy    
mem_load
opsize
opcode        
prnint        
prnintu
prnhex        
prnhexu
prnoct        
prnoctu
prnbin        
prnbinu
prnbinf
prnbinb
fpbin      
fpbind        
prndbl        
prndble
prndblr
prnflt        
prnfltr
prndblx
dblsplit    
fpdinfo
fpfinfo
dec2str
dec2stru    
hex2str
hex2stru    
dbl2str
readint
readflt
readdbl
fpu_stack      
fpu_sflag      
fpu_cflag      
fpu_tag
fpu_reg
fpu_copy    
fpu_precision      
fpu_round      
sse_round      
sse_flags      
prnmmx
dumpmmx
prnxmm        
dumpxmm
clearxmm
prnymm        
dumpymm
clearymm    
prnintd
prnintw
prnintb
str2int
str2dbl
str2flt
dbl2int
int2dbl
rndigit
rndint        
isint      
digiti
digith
digiti_count
digiti_countu
digith_count
digith_countu
factorial
powint        
pow2    
iseven        
isodd      
bconv      
bitfield    
addf    
subf    
mulf    
divf
fcalc    
pow
sqroot        
log10      
ln10    
deg2rad
rad2deg
sine    
tangent
cosine        
sincos        
atangent    
chr_isdigit
chr_isalpha
chr_isupper
chr_islower
chr_toupper
chr_tolower
chr_chcase        
chr_find    
chr_count      
chr_shuffle
ascii      
str_copy    
str_length        
str_cmpz    
str_cmp
str_toupper
str_tolower
str_reverse
str_trim    
str_wordcnt
str_token      
str_find    
str_findz
str_appendz
str_append
sort_byte
sort_int    
sort_dbl    
sort_dblx      
sort_flt    
aprndbl
aprndblx    
aprnflt
aprnint
« Last Edit: August 20, 2016, 12:03:48 AM by stressful »