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;
$ ./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;
;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.