/* program to read strings, convert to numeric, convert back to string and print the results */ #define Op1 %l0 #define Op2 %l1 /*------------------------------- some data declarations -------------------------------*/ .section ".data" hdr: .asciz "This is a Program to do minor arithmetic" msg1: .asciz "Enter a number " msg2: .asciz "Enter an operation " out: .ascii "The answer is: " out1: .skip 11 err1: .asciz "Error: bad input" /*------------------------------ 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 value from user */ nop call cvb /* convert to binary */ nop mov %o0,Op1 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 value from user */ nop call cvb /* convert to binary */ nop mov %o0,Op2 sethi %hi(msg2),%o0 /* load address of msg2 */ or %o0,%lo(msg2),%o0 /* ditto - takes 2 steps */ call prtstring /* print msg1 */ nop call rdstring nop ldub [%o0],%o0 /* character */ cmp %o0,"+" bne not_plus nop add Op1,Op2,Op1 ba done nop not_plus: cmp %l3,"-" bne error nop sub Op1,Op2,Op1 done: mov Op1,%o0 sethi %hi(out1),%o1 /* load address of out1 */ or %o1,%lo(out1),%o1 /* ditto - takes 2 steps */ call cvc nop 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 nop error: sethi %hi(err1),%o0 /* load address of err1 */ or %o0,%lo(err1),%o0 /* ditto - takes 2 steps */ call prtstring nop mov 1,%g1 ta 0 /*------------------- 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); } long cvb(char *in) { long out; sscanf(in,"%ld",&out); return out; } char * cvc(long in, char * out) { spritf(out,"%ld",in); return out; } -----------------------*/