hello, i'm still trying to make the program better, but this time i have a segmentation fault, i added two functions to convert the input number to hexa and from hexa to ascii. @encryptor256: i know it's simple, but for now i'm still a beginner, so i'm doing all i can to understand. here is my code:
SYS_EXIT EQU 1
SYS_WRITE EQU 4
SYS_READ EQU 3 ;buffer
STDOUT EQU 1
STDIN EQU 2
;macro printf
%macro print 2
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, %1 ;expected number
mov edx, %2 ;length
int 0x80
%endmacro
;macro scanf
%macro read 2
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, %1
mov edx, %2
int 0x80
%endmacro
section .data
msg1 db "Entrer le premier nombre: ", 0xA, 0xD
len1 equ $-msg1
msg2 db "Entrer le deuxieme nombre: ", 0xA, 0xD
len2 equ $-msg2
msg3 db "La somme est : ", 0xA, 0xD
len3 equ $-msg3
section .bss
num0 resb 8
num1 resb 8
somme resb 17
section .code
global _start ;doit être déclaré pour utiliser gcc
_start:
print msg1, len1 ;on affiche le premier message
read num0, 8 ;le user entre le premier nombre
print msg2, len2 ;on affiche le deuxième message
read num1, 8 ;le user entre le deuxième nombre
; on effectue le calcul
mov ecx, [num0] ;on mov le premier nombre dans le registre eax
sub ecx, '0' ; convert ascii to number
call numb2hex ; convert number to hex
mov [num0], ecx
xor ecx, ecx
mov ecx, [num1]
sub ecx, '0'
call numb2hex
mov [num1], ecx
xor ecx, ecx
mov eax, [num0]
add [num1], eax
mov ecx, [num1] ;we use ecx register to store the number
;convert somme to ascii and display it
call hex2ascii
mov [somme], ecx
xor ecx, ecx
;on affiche la somme
print msg3, len3
print somme, 17
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
;******************************************************* func NUMB2HEX ***********************************************************************
numb2hex:
;expect number in ecx, and buffer in eax
mov edx, 8 ;initialisation of a loop counter- doing 8 digits
.top:
rol ecx, 4 ;rotating the four leftmost bits to the bottom
mov ebx, ecx ;copying of it, cause, going to trash it
and ebx, 0Fh ;isolating the bits we want to convert
;convert those bits to a character
cmp bl, 9 ;is it bigger than 9
jbe .dec_digit
;if it's > 9, bump it up to A through F
add bl, 7 ;or 27h for lowercase
.dec_digit:
add bl, '0' ;add '0' to convert character representing that digit
mov[eax], bl ;we put it in EAX(our buffer)
inc eax ;prepare for the next character
dec edx ;done?
jnz .top ;if not, do more
ret
;********************************************** func hex2ascii****************************************
hex2ascii:
mov edx, 8
.lm:
rol ecx, 4
mov ebx, ecx
and ebx, 0Fh
cmp bl, 9
jbe .hex_digit
add bl, 7
.hex_digit:
add bl, '0'
mov [eax], bl
inc eax
loop .lm
ret
sorry some comments are in french, i'm a french speaker.
so i just tried with simple number, but it still doesn't work, i know for number with more than two digits i need check if there's an overflow as encryptor256 said, but for now it is a lil bit difficult for me. thanks