R
RSoIsCaIrLiIoA
I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can't seem to get it to work.
do you like my last try?
; nasmw -f obj this_file.asm
; bcc32 this_file.obj
section _DATA public align=4 class=DATA use32
Scrivi_numero_ip db "Srivi un internet adr. > " , 0
Errore db "Errore" , 10 , 0
Errore1 db "Errore" , 0
Errore_linea_troppo_lunga db "Errore linea troppo lunga" , 0
Numero_naturale_conversione_$u
db "Numero naturale conversione %u" , 10 , 0
Errore_contenuto_linea db "Errore contenuto linea" , 10 , 0
$lf db "%lf" , 0
errno_u_$i db "errno=%i " , 0
$u_$u db " <%u %u> " , 0
$i_p$i_p$i_p$i db " %d.%d.%d.%d%*[^" , 10 , "]" , 0
val db 0
extern _printf , _errno , _sscanf , _fgets
extern _perror , __streams , _isspace
global _main
section _TEXT public align=1 class=CODE use32
; unsigned decarr_to_u(char* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
decarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
..a0:
mov dl, [esi]
..a1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a4
cmp edx, 10
je .a4
cmp edx, '.'
je .a4
jmp short .e0
..a2:
lea ebx, [4*eax+eax]
sub edx, '0'
lea eax, [2*ebx]
add eax, edx
inc esi
dec ecx
jnz .a0
..a3: ; errore +3 digits
cmp ecx, 0
jne .a4
cmp dl, 0
je .a4
mov dl, [esi]
cmp edx, '9'
jg .a4
cmp edx, '0'
jl .a4
jmp short .e0
..a4:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret
; unsigned hexarr_to_u(char* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
hexarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
cmp byte[esi], '0'
jne .e0
inc esi
cmp byte[esi], 'x'
je .h0
cmp byte[esi], 'X'
je .h0
jmp .e0
..h0:
inc esi
..a0:
mov dl, [esi]
..a1:
cmp edx, 'F'
ja .c0
cmp edx, 'A'
jb .c0
sub edx, 'A'
add edx, 10
jmp short .a2
..c0:
cmp edx, 'f'
ja .c1
cmp edx, 'a'
jb .c1
sub edx, 'a'
add edx, 10
jmp short .a2
..c1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
sub edx, '0'
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a6
cmp edx, 10
je .a6
cmp edx, '.'
je .a6
jmp short .e0
..a2:
shl eax, 4
inc esi
add eax, edx
dec ecx
jnz .a0
..a3: ; errore +3 digits
cmp ecx, 0
jne .a6
cmp dl, 0
je .a6
mov dl, [esi]
cmp edx, 'F'
jg .a4
cmp edx, 'A'
jl .a4
jmp short .e0
..a4:
cmp edx, 'f'
jg .a5
cmp edx, 'a'
jl .a5
jmp short .e0
..a5:
cmp edx, '9'
jg .a6
cmp edx, '0'
jge .a6
jmp .e0
..a6:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret
; ip_to_ul(dword* ris, dword* arr)
; k=0k, 4Ra, 8ris, 12arr
ip_to_ul:
push ebp
mov ebp, esp ; @w
sub esp, 32 ; 21..24 <32
push ebx
push ecx
push edx
push esi
push edi
%define @ris ebp+8
%define @arr ebp+12
%define @w ebp+24
mov esi, [@arr]
xor ebx, ebx
jmp short .c1
..c0:
inc esi
..c1:
mov bl, [esi]
push ebx
call _isspace
add esp, 4
cmp eax, 0
jne .c0
cmp bl, 0
jne .c2
..e0:
mov eax, [@ris]
mov dword[eax], 0
jmp .fn
..c2: ; c=1.2.3.4
mov ecx, 4
mov edx, 0
mov ebx, 0
..c3:
lea eax, [@w]
push esi
push eax
call decarr_to_u
add esp, 8
cmp esi, [@w]
jne .c4
lea eax, [@w]
push esi
push eax
call hexarr_to_u
add esp, 8
cmp esi, [@w]
je .e0
..c4:
cmp eax, 255
ja .e0
shl ebx, 8
mov esi, [@w]
add ebx, eax
cmp ecx, 1
je .c5
cmp byte[esi], '.'
jne .e0
jmp short .c6
..c5:
cmp byte[esi], ' '
je .c7
cmp byte[esi], 10
je .c7
jmp short .e0
..c6:
inc esi
dec ecx
jmp short .c3
..c7:
mov eax, [@ris]
mov dword[eax], 1
mov eax, ebx
..fn:
%undef @ris
%undef @arr
%undef @w
pop edi
pop esi
pop edx
pop ecx
pop ebx
mov esp, ebp
pop ebp
ret
; k= 0k, 4Ra, 8@argc, 12@argv
_main: ; foo[256], sp[4], w[4]
push ebp ; 11..266, 267..270, 271..274<512
mov ebp, esp
sub esp, 512
push ebx
push esi
push edi
%define @argc ebp+8
%define @argv ebp+12
%define @foo ebp-266
%define @w ebp-274
mov eax, 0
mov ebx, ebp
..l0:
mov byte[ebx], 0
inc eax
dec ebx
cmp eax, 400
jb .l0
..a0:
push Scrivi_numero_ip
call _printf
add esp, 4
lea eax, [@foo]
push __streams
push 256
push eax
call _fgets
add esp, 12
cmp eax, 0
jne .l4
..l1:
push Errore
call _printf
add esp, 4
jmp .fine
..l2:
push Errore_linea_troppo_lunga
call _printf
add esp, 4
jmp .fine
..l3:
push Errore_contenuto_linea
call _printf
add esp, 4
jmp .fine
..l4:
cmp byte[eax+254], 0
jne .l2
lea ebx, [@w]
push eax
push ebx
call ip_to_ul
add esp, 8
cmp dword[@w], 0
je .l3
cmp eax, 0
je .fine
push eax
push Numero_naturale_conversione_$u
call _printf
add esp, 8
jmp .l0
..fine:
mov eax, 0
%undef @argc
%undef @argv
%undef @foo
%undef @w
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret