.makeexe jump main GETCHAR: // read char from keyboard into r1 inch r1 jpos r1, endgetchar pause jump getchar endgetchar: type r1 // input read by INCH is not echoed. If you want to // see what you're typing, you have to echo it yourself. ret // void printstr(char * s) // { int i, c; // i = 0; // while (true) // { c = s[i]; // if (c == 0) // break; // putchar(c); // i += 1; } } PRINTSTR: push fp load fp, sp sub sp, 2 load r1, 0 // i = 0 store r1, [fp-1]; prs_L1: // while (true) load r1, [fp-1] // c = s[i] ldch r1, [fp+2] store r1, [fp-2] load r1, [fp-2] // if (c == 0) comp r1, 0 jcond neq, prs_L2 jump prs_L3 // break prs_L2: type [fp-2] // putchar(c) inc [fp-1] // i += 1 jump prs_L1 // end of while prs_L3: load sp, fp // return pop fp ret // void gets(char * s) // { int i, c; // i = 0; // while (true) // { c = getchar(); // if (c == '\n') // break; // s[i] = c; // i += 1; } // s[i] = 0; } GETS: push fp load fp, sp sub sp, 2 load r1, 0 // i = 0 store r1, [fp-1] gets_L1: // while (true) call getchar store r1, [fp-2] // c = getchar() load r1, [fp-2] // if (c == '\n') comp r1, '\n' jcond neq, gets_L2 jump gets_L3 // break gets_L2: load r1, [fp-1] // s[i] = c load r0, [fp-2] stch r1, [fp+2] inc [fp-1] // i += 1 jump gets_L1 // end of while gets_L3: load r1, [fp-1] // s[i] = 0 load r0, 0 stch r1, [fp+2] load sp, fp // return pop fp ret prompt: .string "type something. " reply: .string "you said \"" end: .string "\"\n" // void main() // { char s[80]; // printstr(prompt); // gets(s); // printstr(reply); // printstr(s); // printstr(end); } MAIN: push fp load fp, sp sub sp, 20 // s starts at FP-20 push prompt call printstr add sp, 1 push fp-20 call gets add sp, 1 push reply call printstr add sp, 1 push fp-20 call printstr add sp, 1 push end call printstr add sp, 1 load sp, fp pop fp ret