/* */ /* Now that you have the source file in the editor, let's begin correcting */ /* the errors in the program. Use the FIND edit command to locate the er- */ /* rors beginning at line 70. Enter F. Enter the string to search for as */ /* shown below: */ /* VALUE = VALUE + CHAR - '0' */ /* NOTE: Press A (AGAIN) if the cursor stops at the string definition above. */ /* */ $title('===> Change Maker Demo <===') Cmaker: DO; DECLARE TRUE literally '0FFH', FALSE literally '0', IN_PORT literally '1', OUT_PORT literally '2', lf_cr (2) BYTE DATA (0AH, 0DH), paid_text (*) BYTE DATA (7, 'Paid = '), purchased_text (*) BYTE DATA (8, 'Price = '), coins WORD, change WORD, dollars WORD, quarters WORD, nickels WORD, dimes WORD, pennies WORD, paid WORD, purchased WORD; $eject read: PROCEDURE BYTE; /*Sets char equal to ASCII byte received*/ DECLARE /*from port 1; writes byte to output port*/ char BYTE; /*also*/ char = INPUT(in_port); RETURN char; END read; write: PROCEDURE (text_ptr, text_cnt); /*Writes text_cnt ASCII bytes */ DECLARE /*that begin at txt_ptr to port2*/ text_ptr POINTER, text_cnt WORD, text BASED text_ptr (1) BYTE, i WORD; DO i = 0 TO text_cnt - 1; OUTPUT(out_port) = text(i); END; END write; write_decimal: PROCEDURE (value);/*Stores decimal digits in the array*/ DECLARE /*digit_stack; calls for their conver-*/ value WORD, /*sion to ASCII*/ i WORD, digit_stack(5) BYTE, stk_top WORD; stk_top = 5; DO WHILE value > 0; digit_stack(stk_top := stk_top - 1) = value MOD 10; value = value/10; END; DO i = stk_top TO 5; /* */ /* To fix this error, we will use the Xchange edit command. Position the */ /* cursor over the 5 in the string "Do i = stk_top". Press X to invoke the */ /* Xchange command. Now, change the 5 to a 4. Press to terminate */ /* this command. */ /* Use the FIND edit command to find the last set of edit instructions. Enter*/ /* F. Enter the string shown below: */ /* QUIT */ /* Press A to search beyond the string shown above. */ /* */ CALL convert_decimal_digit(digit_stack(i)); END; END write_decimal; convert_decimal_digit: PROCEDURE (decimal_digit);/*Converts decimal digit*/ DECLARE /*to ASCII numbers and */ decimal_digit BYTE, /*calls WRITE for each */ ascii_digit BYTE; /*ASCII digit */ ascii_digit = decimal_digit + '0'; CALL write(@ascii_digit, 1); END convert_decimal_digit; print_coin: PROCEDURE (text_ptr, value); /*Formats writing of change */ DECLARE /*values and TEXT; calls */ text_ptr POINTER, /*Write_decimal procedure */ value WORD, text BASED text_ptr STRUCTURE (cnt BYTE, string(1) BYTE), text_cnt BYTE; IF value = 0 THEN RETURN; CALL write (@(' '), 2); CALL write_decimal(value); text_cnt = text.cnt; IF value = 1 THEN text_cnt = text_cnt - 1; /* make singular */ CALL write(@text.string, text_cnt); CALL write(@lf_cr, 2); END print_coin; payment: PROCEDURE; /*Specifies order in which*/ DECLARE /*change information is */ s_text (*) BYTE DATA (8, ' dollars'), /*written; calls */ q_text (*) BYTE DATA (9, ' quarters'), /*formatting procedure */ n_text (*) BYTE DATA (8, ' nickels'), d_text (*) BYTE DATA (6, ' dimes'), p_text (*) BYTE DATA (8, ' pennies'), p1_text (*) BYTE DATA (7, ' penny'); IF (dollars OR quarters OR dimes OR nickels OR pennies) = 0 THEN CALL write(@('No change'), 9); ELSE DO; CALL write(@('Change = '),9); CALL write (@lf_cr, 2); CALL print_coin(@s_text,dollars); CALL print_coin(@q_text,quarters); CALL print_coin(@d_text,dimes); CALL print_coin(@n_text,nickels); IF pennies = 1 THEN CALL print_coin(p1_text,pennies); ELSE CALL print_coin(p_text,pennies); END; END payment; get_input: PROCEDURE (text_ptr) WORD; /*Writes request to output; */ DECLARE /*converts each received ASCII*/ text_ptr POINTER, /*byte to a decimal digit and */ value_ptr POINTER, /*assembles decimal values */ value WORD, char BYTE, not_done BYTE, text BASED text_ptr STRUCTURE (cnt BYTE, string(1) BYTE); CALL write(@text.string, text.cnt); value = 0; not_done = TRUE; DO WHILE not_done; char = read; IF (char >= '0') AND (char <= '9') THEN DO; value = value + char - '0'; value = value / 10; /* */ /* Let's use the REPLACE edit command to fix the errors above. Use the */ /* key to position the cursor at the beginning of the string */ /* VALUE = VALUE + CHAR - '0'. The line should define VALUE as */ /* VALUE = VALUE * 10 */ /* You only need to replace to the right of the second VALUE. Enter R to in-*/ /* voke the REPLACE command. The editor will prompt you with: REPLACE"". */ /* Enter the portion of the string you wish to replace as shown below: */ /* + CHAR - '0' */ /* When you press the key, the editor responds by asking you for the */ /* string with which to replace + CHAR - '0'. Enter */ /* * 10 */ /* Now let's replace the string / 10 in the next line of code with the */ /* string + CHAR - '0'. Enter R. The last REPLACE command will be shown. */ /* Enter the command as shown below (typing over the old REPLACE command): */ /* / 10 + CHAR - '0' */ /* Use the -find edit command to find the last error. Enter - (for -find). */ /* Enter the string to search for as shown below: */ /* DO I = STK_TOP TO 5 */ /* */ END ELSE not_done = FALSE; END; RETURN alue; END get_input; begin:; DO; CALL write (@lf_cr, 2); paid = get_input(@paid_text); purchased = get_input(@purchased_text); CALL write (@lf_cr, 2); change = paid - purchased; dollars = change/100; coins = change MOD 100; quarters = coins/25; coins = coins MOD 25; dimes = coins/10; coins = coins MOD 10; nickels = coins/5; pennies = coins MOD 5; IF paid < purchased THEN CALL write(@('NO CHEATING!'), 12); ELSE CALL payment; END; exit:; HALT; END; /* QUIT */ /* You are now finished with correcting the source errors. All that remains */ /* is to exit the editor and return to the tutorial. You can save this */ /* error free source file or you can exit without saving the file. If you */ /* want to save this file, write it to a file other than CMKER2.S2 so that */ /* you preserve the tutorial file unchanged for future users of the tutorial.*/ /* If you want to save the file, perform the following steps: */ /* 1. Enter Q (for QUIT). */ /* 2. Enter W (for WRITE). */ /* 3. Enter the file name to which you wish to write the edited source */ /* code. */ /* 4. Press the key to terminate the WRITE command. */ /* 5. Enter A (for ABORT) to exit the editor. NOTE: When the I2ICE prompt */ /* (*) reappears on the screen, enter N to continue with the */ /* tutorial. */ /* If you do not want to save this source file, perform steps 1 and 5 above, */ /* answering "Y" to the request: all changes lost? (y or [n]). */