/* program to convert infix to polish suffix */
/* this program has a .S (not .s) extension */
#define InPtr %l0
#define NxtByte %l1
/*-------------------------------
some data declarations
-------------------------------*/
.section ".data"
hdr: .asciz "This is a Program to convert infix to suffix"
msg1: .asciz "Enter an expression "
out: .skip 80 /* output area */
/*------------------------------
the program
-------------------------------*/
.section ".text"
.align 4
.global main
main:
save %sp,-64,%sp
sethi %hi(hdr),%o0 /* load address of greeting */
or %o0,%lo(hdr),%o0 /* ditto - takes 2 steps */
call prtstring /* print greeting */
nop /* be careful of these */
sethi %hi(msg1),%o0 /* load address of msg1 */
or %o0,%lo(msg1),%o0 /* ditto - takes 2 steps */
call prtstring /* print msg1 */
nop
call rdstring /* read an expression from user */
nop
mov %o0,InPtr /* move addr input string to InPtr */
sethi %hi(out),%l2 /* load address of out */
or %l2,%lo(out),%l2 /* ditto - takes 2 steps */
loop:
ldub [InPtr],NxtByte /* load byte pointed to by InPtr */
cmp NxtByte,"$" /* is it a ( ? */
be done /* end */
nop
cmp NxtByte,"(" /* is it a ( ? */
bne not_opn /* no */
nop
add InPtr,1,InPtr /* increment address */
ba loop /* get next */
nop
not_opn:
cmp NxtByte,"+" /* is it a + ? */
be opr /* yes */
nop
cmp NxtByte,"*"
be opr
nop
cmp NxtByte,"-"
be opr
nop
cmp NxtByte,"/"
be opr
nop
ba not_opr
nop
opr: sub %sp,1,%sp /* inc stack */
stb NxtByte,[%sp] /* push */
add InPtr,1,InPtr /* increment address */
ba loop /* get next */
nop
not_opr:
cmp NxtByte,")" /* is it a close */
bne not_close
nop
ldub [%sp],%l3 /* load byte from stack */
add %sp,1,%sp /* decrement stack pointer */
stb %l3,[%l2] /* store in output */
add %l2,1,%l2 /* increment pointer */
add InPtr,1,InPtr /* increment address */
ba loop /* get next */
nop
not_close:
stb NxtByte,[%l2] /* move byte to output */
add %l2,1,%l2 /* increment pointer */
add InPtr,1,InPtr /* increment pointer */
ba loop /* get next */
nop
done:
stb %g0,[%l2]
sethi %hi(out),%o0 /* load address of out */
or %o0,%lo(out),%o0 /* ditto - takes 2 steps */
call prtstring
nop
mov 1,%g1
ta 0
/*-------------------
The C functions (compiled separately):
int rdnum() { int i; scanf("%d",&i); return(i); }
prntnum(int i) { printf("Answer is %d\n",i); }
char * rdstring() { static char x[256]; gets(x); return(&x); }
void prtstring(char *x) { printf("%s\n",x); }
--------------------*/