Re: why this assembly language is not good?



On Mon, 10 Apr 2006 23:59:00 +0200, Herbert Kleebauer <klee@xxxxxxxxx>
wrote:

this is the nasm version
it seems "(" ")" give error don't know why.

section _DATA public align=4 class=DATA use32

fatto dd 0 , 0
fatto1 dd 0 , 0
dieci dd 10 , 0
tab times 258 dd 0
vai times 258 dd m_via

Nerrore_$iN db 10 , "error %i" , 10 , 0 , 0

max equ 99

global StrToInt
global StrToUns
global getNum
global _cf
global get_w

extern _printf , _exit

section _TEXT public align=1 class=CODE use32

; void delimita(void)
; inserisce i *caratteri*
; usati da tkn_line (sotto forma di stringa)
; s=0j, 4b, 8a, 12ra
delimita_a:
push eax
push ebx
push edi
cmp dword[fatto], 0
jne .in
call delimita
mov dword[fatto], 1
..in:
mov eax, vai
; ! : 0x00 (%000000 00)
mov dword[eax], m_lneg

; ~ : 0x01 (%000000 01)
mov edi, 1
mov dword[eax+4*edi], m_not

; - : 0x02 (%000000 10)
mov edi, 2
mov dword[eax+4*edi], m_neg

; * : 0x04 (%000001 00)
mov edi, 4
mov dword[eax+4*edi], m_mul

; / : 0x05 (%000001 01)
mov edi, 5
mov dword[eax+4*edi], m_div

; \ : 0x06 (%000001 10)
mov edi, 6
mov dword[eax+4*edi], m_mod

; + : 0x08 (%000010 00)
mov edi, 8
mov dword[eax+4*edi], m_add

; - : 0x09 (%000010 01)
mov edi, 9
mov dword[eax+4*edi], m_sub

; << : 0x0c (%000011 00)
mov edi, 0x0c
mov dword[eax+4*edi], m_shfm

; >> : 0x0d (%000011 01)
mov edi, 0x0d
mov dword[eax+4*edi], m_shfd

; < : 0x10 (%000100 00)
mov edi, 0x10
mov dword[eax+4*edi], m_minore

; > : 0x11 (%000100 01)
mov edi, 0x11
mov dword[eax+4*edi], m_maggiore

; <= : 0x12 (%000100 10)
mov edi, 0x12
mov dword[eax+4*edi], m_minore_u

; >= : 0x13 (%000100 11)
mov edi, 0x13
mov dword[eax+4*edi], m_maggiore_u

; == : 0x14 (%000101 00)
mov edi, 0x14
mov dword[eax+4*edi], m_uguale

; != : 0x15 (%000101 01)
mov edi, 0x15
mov dword[eax+4*edi], m_diverso

; & : 0x18 (%000110 00)
mov edi, 0x18
mov dword[eax+4*edi], m_and

; ^ : 0x1c (%000111 00)
mov edi, 0x1c
mov dword[eax+4*edi], m_xor

; | : 0x20 (%001000 00)
mov edi, 0x20
mov dword[eax+4*edi], m_or

; && : 0x24 (%001001 00)
mov edi, 0x24
mov dword[eax+4*edi], m_andu

; || : 0x28 (%001010 00)
mov edi, 0x28
mov dword[eax+4*edi], m_oru

; case 0x30: goto weiter;
mov edi, 0x30
mov dword[eax+4*edi], m_special
pop edi
pop ebx
pop eax
ret

; 0 is the ra
m_special:
mov dword[esp], get_w.c0
ret


; void delimita_a(void)
; inserisce i *caratteri*
; usati da tkn_line (sotto forma di stringa)
; s=0j, 4b, 8a, 12ra
delimita:
push eax
push ebx
push edi
mov eax, tab
mov edi, 0
..c0: ; 0..255
mov dword[eax+4*edi], 255
inc edi
cmp edi, 255
jbe .c0
mov ebx, '0'
mov edi, 0
..c1:
mov dword[eax+4*ebx], edi
inc ebx
inc edi
cmp ebx, '9'
jbe .c1
mov ebx, 'A'
mov edi, 10
..c2:
mov dword[eax+4*ebx], edi
inc ebx
inc edi
cmp ebx, 'Z'
jbe .c2
mov ebx, 'a'
mov edi, 10
..c3:
mov dword[eax+4*ebx], edi
inc ebx
inc edi
cmp ebx, 'z'
jbe .c3
pop edi
pop ebx
pop eax
ret


; int cf(void);
_cf:
mov eax, 0
adc eax, 0
ret


; eax __stdcall StrToInt(char* string, char** pos, int base)
; eg. char buf[256]={0}, *pc; int val=StrToInt(buf, &pc, 10);
; CF== carry flag
; format==<spaces|empy><+|-|empy><digits> where spaces={' ', '\t'}
; convert the number in format above in the 0 terminated
; string "string" in the base format in a integer in the range
; [-2147483648, 2147483647] that return in eax.
; 2<=base<=36 possible digits "01234567890[A-Z][a-z]"
; if there is not a number in the above format then
; after the call => (string==pos and CF==1)
; if overflow for number > 0 return 2147483647 CF==1
; if overflow for number < 0 return -2147483648 CF==1
; if there is (or not) overflow it read all the number
; and save in "pos" the first not digit position
; if all is ok CF==0 if error CF==1
; 0k, 4i, 8r, 12c, 16b, 20Ra, 24string, 28pos, 32base
StrToInt:
push ebx
push ecx
push edx
push esi
push ebp
%define @string [esp+24]
%define @pos [esp+28]
%define @base [esp+32]
cmp dword[fatto], 0
jne .in
call delimita
mov dword[fatto], 1
..in:
mov esi, @string
cmp esi, 0
je .ce
xor ebx, ebx
mov ecx, @base
cmp ecx, 36
ja .ce
cmp ecx, 1
jbe .ce
mov ebp, 0 ; in k il segno
jmp short .c1
..c0:
inc esi
..c1:
cmp byte[esi], ' '
je .c0
cmp byte[esi], 9
je .c0
cmp byte[esi], '+'
jne .c2
inc esi
jmp short .c3
..c2:
cmp byte[esi], '-'
jne .c3
inc esi
inc ebp
..c3:
mov bl, [esi]
cmp [tab+4*ebx], ecx
jae .ce
jmp short .c5
..ce:
mov eax, @pos
mov edx, @string
mov [eax], edx
mov eax, 0
jmp .ca
..c4:
inc esi
..c5:
cmp byte[esi], '0'
je .c4
mov eax, 0
..c6:
mov bl, [esi]
mov edx, [tab+4*ebx]
cmp edx, ecx
jae .c8
mov ebx, edx
mul ecx ; in ci +/-oo
cmp edx, 0
jne .c7
add eax, ebx
jc .c7
inc esi
jmp short .c6
..c7:
inc esi
mov bl, [esi]
mov edx, [tab+4*ebx]
cmp edx, ecx
jb .c7
..ci:
mov edx, 1
mov eax, 0x7FFFFFFF
jmp short .a0
..c8:
mov edx, 0
cmp eax, 0x80000000
jb .a0
ja .ci
cmp ebp, 1
je .c9 ; a=0x80000000
jmp short .ci
..a0:
cmp ebp, 1
jne .c9
cmp edx, 1
jne .a1
inc eax
jmp short .c9
..a1:
neg eax
..c9:
mov ebx, @pos
mov [ebx], esi
cmp edx, 0
jne .ca
clc
jmp short .cf
..ca:
stc
..cf:
%undef @string
%undef @pos
pop ebp
pop esi
pop edx
pop ecx
pop ebx
ret 12

; eax __stdcall StrToUns(char* string, char** pos, int base)
; eg. char buf[256]={0}, *pc; int val=StrToUns(buf, &pc, 10);
; CF== carry flag
; format==<spaces|empy><+|empy><digits> where spaces={' ', '\t'}
; convert the number in format above in the 0 terminated
; string "string" in the base format in a integer in the range
; [0, 0xffffffff] that return in eax.
; 2<=base<=36 possible digits "01234567890[A-Z][a-z]"
; if there is not a number in the above format then
; after the call => (string==pos and CF==1)
; if overflow return 0xffffffff CF==1
; if there is (or not) overflow it read all the number
; and save in "pos" the first not digit position
; if all is ok CF==0 if error CF==1
; 0i, 4r, 8c, 12b, 16ra, 20string, 24pos, 28base
StrToUns:
push ebx
push ecx
push edx
push esi
%define @string [esp+20]
%define @pos [esp+24]
%define @base [esp+28]
cmp dword[fatto], 0
jne .in
call delimita
mov dword[fatto], 1
..in:
mov esi, @string
cmp esi, 0
je .ce
xor ebx, ebx
mov ecx, @base
cmp ecx, 36
ja .ce
cmp ecx, 1
jbe .ce
jmp short .c1
..c0:
inc esi
..c1:
cmp byte[esi], ' '
je .c0
cmp byte[esi], 9
je .c0
cmp byte[esi], '+'
jne .c2
inc esi
..c2:
mov bl, [esi]
cmp [tab+4*ebx], ecx
jae .ce
jmp short .c5
..ce:
mov eax, @pos
mov edx, @string
mov [eax], edx
mov eax, 0
jmp .ca
..c4:
inc esi
..c5:
cmp byte[esi], '0'
je .c4
mov eax, 0
..c6:
mov bl, [esi]
mov edx, [tab+4*ebx]
cmp edx, ecx
jae .c8
mov ebx, edx
mul ecx ; in ci +/-oo
cmp edx, 0
jne .c7
add eax, ebx
jc .c7
inc esi
jmp short .c6
..c7:
inc esi
mov bl, [esi]
mov edx, [tab+4*ebx]
cmp edx, ecx
jb .c7
..ci:
mov edx, 1
mov eax, 0xFFFFFFFF
jmp short .c9
..c8:
mov edx, 0
..c9:
mov ebx, @pos
mov [ebx], esi
cmp edx, 0
jne .ca
clc
jmp short .cf
..ca:
stc
..cf:
%undef @string
%undef @pos
pop esi
pop edx
pop ecx
pop ebx
ret 12


; CF __stdcall getNum(char **pp, int *lip);
; CF==0 not get, CF==1 get
; format = %<digits_01> binario
; format = $<digits_09AZ> esadecimale
; format = <digits_09> decimale
; format = '4letters'
; s=0i, 4b, 8a, 12ra, 16pp, 20lip
getNum:
push eax
push ebx
push esi
%define @pp [esp+16]
%define @lip [esp+20]
cmp dword[fatto], 0
jne .in
call delimita
mov dword[fatto], 1
..in:
mov eax, @pp
mov esi, [eax]
mov ebx, 0
cmp byte[esi], '%'
jne .c0
inc esi
mov bl, [esi]
cmp dword[tab+4*ebx], 1
ja .mce
push 2
push eax
push esi
call StrToUns
jmp .cf
..c0:
cmp byte[esi], '$'
jne .c1
inc esi
mov bl, [esi]
cmp dword[tab+4*ebx], 15
ja .ce
push 16
push eax
push esi
call StrToUns
jmp short .cf
..mce:
jmp short .ce
..c1:
cmp byte[esi], '0'
jb .c2
cmp byte[esi], '9'
ja .c2
mov bl, [esi]
cmp dword[tab+4*ebx], 9
ja .ce
push 10
push eax
push esi
call StrToUns
jmp short .cf
..c2: ; ' 1 2 3 4 '
mov ebx, 0
cmp byte[esi], "'"
jne .ce
cmp byte[esi+5], "'"
jne .ce
inc esi
mov bl, [esi]
mov eax, ebx
inc esi
mov bl, [esi]
shl eax, 8
add eax, ebx
inc esi
mov bl, [esi]
shl eax, 8
add eax, ebx
inc esi
mov bl, [esi]
shl eax, 8
add eax, ebx
add esi, 2
mov ebx, @pp
mov [ebx], esi
clc
jmp short .cf
..ce:
mov eax, 0
stc
..cf:
mov ebx, @lip
mov [ebx], eax
%undef @pp
%undef @lip
pop esi
pop ebx
pop eax
ret 8

; a=1, r=2, use c

; ! : 0x00 (%000000 00)
m_lneg:
inc eax
cmp eax, 1
je .a0
mov eax, 0
..a0:
clc
ret

; ~ : 0x01 (%000000 01)
m_not:
not eax
clc
ret

; - : 0x02 (%000000 10)
m_neg:
neg eax
jo .a0
clc
jmp short .cf
..a0:
stc
..cf:
ret

; * : 0x04 (%000001 00)
m_mul:
imul edx
jo .a0
clc
jmp short .cf
..a0:
stc
..cf:
ret

; / : 0x05 (%000001 01)
m_div:
push ecx
cmp edx, 0
jne .a0
stc
jmp short .cf
..a0:
mov ecx, edx
mov edx, 0
idiv ecx
clc
..cf:
pop ecx
ret

; \ : 0x06 (%000001 10)
m_mod:
push ecx
cmp edx, 0
jne .a0
stc
jmp short .cf
..a0:
mov ecx, edx
mov edx, 0
idiv ecx
clc
mov eax, edx
..cf:
pop ecx
ret


; + : 0x08 (%000010 00)
m_add:
add eax, edx
jo .a0
clc
jmp short .cf
..a0:
stc
..cf:
ret

; - : 0x09 (%000010 01)
m_sub:
sub eax, edx
jo .a0
clc
jmp short .cf
..a0:
stc
..cf:
ret


; << : 0x0c (%000011 00)
m_shfm:
push ecx
mov ecx, edx
shl eax, cl
clc
pop ecx
ret

; >> : 0x0d (%000011 01)
m_shfd:
push ecx
mov ecx, edx
shr eax, cl
clc
pop ecx
ret

; < : 0x10 (%000100 00)
m_minore:
cmp eax, edx
jge .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret

; > : 0x11 (%000100 01)
m_maggiore:
cmp eax, edx
jle .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret


; <= : 0x12 (%000100 10)
m_minore_u:
cmp eax, edx
jg .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret


; >= : 0x13 (%000100 11)
m_maggiore_u:
cmp eax, edx
jl .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret

; == : 0x14 (%000101 00)
m_uguale:
cmp eax, edx
jne .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret

; != : 0x15 (%000101 01)
m_diverso:
cmp eax, edx
je .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret


; & : 0x18 (%000110 00)
m_and:
and eax, edx
clc
ret

; ^ : 0x1c (%000111 00)
m_xor:
xor eax, edx
clc
ret

; | : 0x20 (%001000 00)
m_or:
or eax, edx
clc
ret

; && : 0x24 (%001001 00)
m_andu:
cmp eax, 0
je .a0
cmp edx, 0
je .a0
mov eax, 1
jmp short .cf
..a0:
mov eax, 0
..cf:
clc
ret

; || : 0x28 (%001010 00)
m_oru:
cmp eax, 0
je .a0
mov eax, 1
jmp short .cf
..a0:
cmp edx, 0
je .a1
mov eax, 1
jmp short .cf
..a1:
mov eax, 0
..cf:
clc
ret

m_via:
push 6
call ende
add esp, 4
ret


; int __stdcall get_w(char *p, int *li);
get_w: ; se s-=16 => 24+16+4 = 44

push ebx
push ecx
push edx
push esi
push edi
push ebp
sub esp, 1024 ; 24+1024+4=1052
%define @p [esp+1052]
%define @li [esp+1056]
%define @la [esp+4]
%define @ca [esp+408]
; s= 404..4[100ints] 808..408[100ints]
%define @ind_p [esp+816]
%define @kk [esp+824]
%define @var [esp+832]
cmp dword[fatto1], 0
jne .in
call delimita_a
mov dword[fatto1], 1
..in:
lea ebx, @la
lea ebp, @ca
mov ecx, @p
mov dword[ebp], 0x34
mov esi, 0
mov edi, 0
..c0: ; w1_0

cmp esi, max
jle .c1
push 1
call ende
add esp, 4
..c1:
cmp edi, max
jle .c2
push 1
call ende
add esp, 4
..c2:
xor eax, eax
mov al, [ecx]
cmp eax, 0
jne .c3
push 2
call ende
add esp, 4
..c3:
cmp eax, '('
jne .c4
mov dword[ebp+4*esi+4], 0x30
inc esi
inc ecx
jmp short .c0
..c4:
cmp eax, '!'
jne .c5
mov dword[ebp+4*esi+4], 0x00
inc esi
inc ecx
jmp short .c0
..c5:
cmp eax, '~'
jne .c6
mov dword[ebp+4*esi+4], 0x01
inc esi
inc ecx
jmp .c0
..c6:
cmp eax, '-'
jne .c7
mov dword[ebp+4*esi+4], 0x02
inc esi
inc ecx
jmp .c0
..c7:
cmp eax, '$'
je .c8
cmp al, '%'
je .c8
cmp al, "'"
je .c8
cmp dword[tab+4*eax], 9
jbe .c8
push 3
call ende
add esp, 4
..c8:
lea edx, @ind_p
mov [edx], ecx
inc edi
lea eax, [ebx+4*edi]
push eax
push edx
call getNum
jnc .1
push 900
call ende
add esp, 4
..1:
mov ecx, [edx]
..c9: ; w1_2

xor eax, eax
mov al, [ecx]
inc ecx
..a0:
cmp eax, '*'
jne .a1
mov eax, 0x04
jmp .af
..a1:
cmp eax, '/'
jne .a2
mov eax, 0x05
jmp .af
..a2:
cmp eax, '\'
jne .a3
mov eax, 0x06
jmp .af
..a3:
cmp eax, '+'
jne .a4
mov eax, 0x08
jmp .af
..a4:
cmp eax, '-'
jne .a5
mov eax, 0x09
jmp .af
..a5:
cmp eax, '<'
jne .a6
cmp byte[ecx], '<'
jne .b0
mov eax, 0x0c
inc ecx
jmp .af
..b0:
cmp byte[ecx], '='
jne .b1
mov eax, 0x12
inc ecx
jmp .af
..b1:
mov eax, 0x10
jmp .af
..a6:
cmp eax, '>'
jne .a7
cmp byte[ecx], '>'
jne .b2
mov eax, 0x0d
inc ecx
jmp .af
..b2:
cmp byte[ecx], '='
jne .b3
mov eax, 0x13
inc ecx
jmp .af
..b3:
mov eax, 0x11
jmp .af
..a7:
cmp eax, '='
jne .a8
cmp byte[ecx], '='
jne .b4
mov eax, 0x14
inc ecx
jmp .af
..b4:
push 4
call ende
add esp, 4
..a8:
cmp eax, '!'
jne .a9
cmp byte[ecx], '='
jne .b5
mov eax, 0x15
inc ecx
jmp .af
..b5:
push 5
call ende
add esp, 4
..a9:
cmp eax, '&'
jne .aa
cmp byte[ecx], '&'
jne .b6
mov eax, 0x24
inc ecx
jmp .af
..b6:
mov eax, 0x18
jmp .af
..aa:
cmp eax, '|'
jne .ab
cmp byte[ecx], '|'
jne .b7
mov eax, 0x28
inc ecx
jmp short .af
..b7:
mov eax, 0x20
jmp short .af
..ab:
cmp eax, '^'
jne .ac
mov eax, 0x1c
jmp short .af
..ac:
cmp eax, ')'
jne .ad
mov eax, 0x31
jmp short .af
..ad:
cmp eax, 0
jne .ae
mov eax, 0x2c
jmp short .af
..ae:
push 6
call ende
add esp, 4
..af:
mov @kk, eax
jmp .h0

..ag:
cmp edi, 1
jge .ah
push 7
call ende
add esp, 4
..ah:
mov eax, [ebp+4*esi]
dec esi
cmp eax, 0x30
jne .ai
jmp .c0
..ai:
mov @var, ebp
mov ebp, eax
cmp eax, 2
ja .al
mov eax, [ebx+4*edi]
call [vai+4*ebp]
mov [ebx+4*edi], eax
jmp short .2
..al:
dec edi
mov eax, [ebx+4*edi]
mov edx, [ebx+4*edi+4]
call [vai+4*ebp]
..2:
mov ebp , @var
mov [ebx+4*edi], eax
..h0:
mov edx, @kk
or edx, 0x03
cmp edx, [ebp+4*esi]
jae .ag

..h1:
mov eax, @kk
cmp eax, 0x2c
je .fe
inc esi
mov dword[ebp+4*esi], eax
jmp .c0

..fe:
cmp esi, 0
je .ff
push 9
call ende
add esp, 4
..ff:
dec edi
cmp edi, 0
je .fg
push 9
call ende
add esp, 4
..fg:
lea esi, @li
mov eax, [ebx+4]
mov edx, [esi]
mov [edx], eax
mov ecx, eax
and eax, 0xFFFFFF00
jnz .fh
mov eax, 1
jmp short .cf
..fh:
mov eax, ecx
neg eax
and eax, 0xFFFFFF00
jnz .fi
mov eax, 1
jmp short .cf
..fi:
mov eax, ecx
and eax, 0xFFFF0000
jnz .fl
mov eax, 2
jmp short .cf
..fl:
mov eax, ecx
neg eax
and eax, 0xFFFF0000
jnz .fm
mov eax, 2
jmp short .cf
..fm:
mov eax, 3
..cf:
%undef @p
%undef @li
%undef @la
%undef @ca
%undef @ind_p
%undef @kk
%undef @var
add esp, 1024
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
ret 8

; void ende(int i);
ende:
mov eax, [esp+4]
push eax
push Nerrore_$iN
call _printf
add esp, 8
push 0
call _exit
add esp, 4
ret


#include <stdio.h>
#include <stdlib.h>

int __stdcall get_w(char* p, int* li);

int main(int c, char** a)
{int li, i, j, k=2020;
char *s1, *s2, h[2048]={0};

s1 = "-123+4*5" ;
//s2 = "(1<<4)+'a bc'-%101010*$1bc&7-(2*3)" ;

get_w(s1,&li); printf("\n %s = %ii : %uu : 0x%x", s1, li,li,li);
//get_w(s2,&li); printf("\n %s = %ii : %uu : 0x%x", s2, li,li,li);

for(j=0, i=1; i<c && k>0; ++i)
{if(a[i])
for(s1=a[i]; *s1&&k>0; ++s1)
{h[j++]=*s1; --k;}
}
get_w(h,&li); printf("\n %s = %ii : %uu : 0x%x", h, li,li,li);
return 0;
}


.



Relevant Pages