yesterday and today i had some free time for spend in this
because in the algo book i have it use push/pop for infixtopolish
(nor that i have understod well what this polish syntax is)
i choose the assembly implementation
(don't know how many bug there are)
C:>infixto
((10+(113-(2+((((((2/(4+3)))))))+2+2))))*a
start [((10+(113-(2+((((((2/(4+3)))))))+2+2))))*a
]; result [10 113 2 2 4 3 + / + - + 2 2 + + a *]
is there some error below?
C:>infixto
(a+b)*c
start [(a+b)*c
]; result [a b + c *]
C:>infixto
a+(b*c)
start [a+(b*c)
]; result [a b c * +]
C:>infixto
a+b+c
start [a+b+c
]; result [a b c + +]
it seems this is build to result
for "right to left" evalutation
when there are no "()"
-------------------------------
; reference in Robert Sedgewick
; nasmw -fobj thisfile.asm
; bcc32 thisfile.obj
%define P _printf
section _DATA use32 public class=DATA
global _main
extern _printf
extern _fgets
extern __streams
; _streams+ 0 = stdin
; _streams+24 = stdout
%define Space 6
%define Digit 0
%define Operator 3
ptr: dd v1, v2, v3, v4, v5, v6
; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Table:
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 6 ; 0
db 6, 16, 16, 6, 16, 16, 16, 16, 16, 16 ; 1 10 e' fine input non spazio
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 2
db 16, 16, 6, 16, 16, 16, 16, 3, 3, 16 ; 3
db 1, 2, 3, 3, 16, 3, 16, 3, 0, 0 ; 4 "." e' lettera
db 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ; 5
db 16, 16, 16, 16, 16, 0, 0, 0, 0, 0 ; 6
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; 7
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; 8
db 0, 16, 16, 16, 3, 0, 16, 0, 0, 0 ; 9
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; 10
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; 11
db 0, 0, 0, 16, 3, 16, 16, 16, 16, 16 ; 12
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 13
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 14
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 15
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 16
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 17
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 18
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 19
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 20
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 21
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 22
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 23
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 24
db 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 ; 25
IstartIIIsIIIresultIIIsIIIn
db "start [%s]; result [%s] " , 13, 10, 0
IErrorIinIcharIIdIIErrstrIIsIn
db "Error in char %d str=%s" , 13, 10, 0
v1:
IErroreIdiIargomenti
db "Errore di argomenti" , 0
v2:
IErroreItroppiIoperatoriIII4028I
db "Errore troppi operatori (>4028)" , 0
v3:
IErroreIoverflowIresult
db "Errore overflow result" , 0
v4:
IErroreIIIIIIIIInonIbilanciate
db "Errore '(' ')' non bilanciate" , 0
v5:
IErroreIalternanzaInonIrispettata
db "Errore alternanza non rispettata" , 0
v6:
IErroreIcarattereInonIpermesso
db "Errore carattere non permesso" , 0
section _BSS use32 public class=BSS
section _TEXT use32 public class=CODE
;Errore se ritorna !=0
; Caso 1: Errore di argomenti
; Caso 2: Errore troppi operatori (>4028)
; Caso 3: Errore overflow result
; Caso 4: Errore '(' ')' non bilanciate
; Caso 5: Errore alternanza non rispettata
; Caso 6: Errore carattere non permesso
;0k,4j,8i,12b,16ra,20Pres,24PresSz,28P_PLen,32P_i+16
; 36 40 44 48
align 4
infixToPolishAsm:
push ebx
push esi
push edi
push ebp
sub esp, 16
mov edi, dword[esp+ 36]
mov ebp, dword[esp+ 40]
mov esi, dword[esp+ 48]
mov ecx, esp
mov dword[esp], 0
mov dword[esp+4], 0
mov dword[esp+8], 0
xor eax, eax
xor ebx, ebx
mov edx, 1
cmp dword[esp+ 44], 0
je .e0
cmp edi, 0
je .e
cmp ebp, 0
jle .e
cmp esi, 0
je .e
jmp .3
.e: sub esi, dword[esp+ 48]
mov edi, dword[esp+ 44]
mov [edi], esi
.e0: mov eax, edx
stc
jmp .z
.e1: mov esp, ecx
mov byte[edi], 0
mov edx, 2
jmp short .e ; errore: overflow stack
.e2: mov esp, ecx
mov byte[edi], 0
mov edx, 3
jmp short .e ; errore: overflow res
.e3: mov esp, ecx
mov byte[edi], 0
mov edx, 4
jmp short .e ; errore: ( non bilanciate
.e4: mov esp, ecx
mov byte[edi], 0
mov edx, 5
jmp short .e ; errore: alternanza non rispettata
.e5: mov esp, ecx
mov byte[edi], 0
mov edx, 6
jmp short .e ; errore: carattere non permesso
.0: mov byte[edi], 0
cmp dword[ecx], 0
jne .e3
cmp dword[ecx+4], 1
jne .e4
.0x: cmp ecx, esp
je .0a
pop edx
mov byte[edi], ' '
inc edi
dec ebp
jle .e2
mov byte[edi], dl
dec ebp
jle .e2
inc edi
mov byte[edi], 0
jmp short .0x
.0a: sub edi, dword[esp+ 36]
mov edx, dword[esp+ 44]
mov [edx], edi
xor eax, eax
jmp .z
.2: inc esi
.3: mov al, [esi]
.0b: cmp eax, 0
je .0
mov bl, [Table+eax]
cmp ebx, Space
je .2
cmp ebx, Digit
jne .2a
add dword[ecx+4], 1
cmp dword[ecx+4], 1
jg .e4
.1a: mov [edi], al
inc esi
mov al, [esi]
inc edi
dec ebp
jle .e2
cmp byte[Table+eax], 0
je .1a
jmp short .0b
.2a: cmp ebx, Operator
jne .4
sub dword[ecx+4], 1
cmp dword[ecx+4], 0
jl .e4
mov edx, ecx
sub edx, esp
shr edx, 2
cmp edx, 4028
jae .e1
push eax
.3a: mov byte[edi], ' '
inc edi
dec ebp
jle .e2
jmp .2
.4: cmp eax, ')'
jne .5
cmp dword[ecx], 0
je .e3
inc dword[ecx]
cmp esp, ecx
ja .e
jz .2
pop edx
sub ebp, 2
jle .e2
mov byte[edi], ' '
inc edi
mov byte[edi], dl
inc edi
jmp .2
.5: cmp eax, '('
jne .e5
dec dword[ecx]
jmp .2
.z:
lea esp, [esp+16]
pop ebp
pop edi
pop esi
pop ebx
ret 16
align 4
_main:
pushad
sub esp, 4048
mov edi, esp
lea esi, [edi+2000]
lea ebp, [edi-4]
push __streams
push 1024
push esi
call _fgets
add esp, 12
cmp eax, 0
je .x
jmp short .1
.e: dec eax
cmp eax, 5
ja .x
mov edx, [ptr+4*eax]
mov eax, [ebp]
push edx
push eax
push IErrorIinIcharIIdIIErrstrIIsIn
call P
add esp, 12
jmp short .x
.1: push esi
push ebp
push 1024
push edi
call infixToPolishAsm
cmp eax, 0
jne .e
push edi
push esi
push IstartIIIsIIIresultIIIsIIIn
call P
add esp, 12
.x:
add esp, 4048
popad
xor eax, eax
ret