// simplemultiply.iml (c) 2000 Kari Laitinen // The following program reads two single-digit numbers (i.e. two // ASCII codes) from the keyboard and multiplies them. In the end // the program displays the result of multiplication on the screen. beginning_of_program: set_memory_pointer text_for_number_input call_subroutine display_text call_subroutine read_and_echo_a_character subtract_value_from_register_a '0' move_content_of_register_a_to_b set_memory_pointer text_for_number_input call_subroutine display_text call_subroutine read_and_echo_a_character subtract_value_from_register_a '0' // Now, the numbers to be multiplied are in registers A and B. // The value '0' (30H was subtracted from both numbers in order // to convert ASCII codes to real binary numbers. // Next we store the numbers in registers A and B into memory // locations labeled "multiplier" and "multiplicand". // The memory location "multiplication_result" will be // used to store the result of multiplication. That location // will be zero in the beginning. set_memory_pointer multiplier store_register_a_to_memory set_memory_pointer multiplicand store_register_b_to_memory load_register_a_with_value 0 set_memory_pointer multiplication_result store_register_a_to_memory multiply_numbers: set_memory_pointer multiplier load_register_a_from_memory jump_if_register_a_zero multiplication_ready decrement_register_a store_register_a_to_memory set_memory_pointer multiplication_result load_register_a_from_memory add_register_b_to_a store_register_a_to_memory jump_to_address multiply_numbers multiplication_ready: set_memory_pointer multiplication_result load_register_a_from_memory // Now the multiplication result is in register A. // Because we were multiplying single-digit numbers, the // maximum multiplication result is 81. For this reason // we will display two digits, the first of which may be // zero in the case of small multiplication results. // To find out what is the more significant digit of the // multiplication result, we must test how many times the // number 10 can be subtracted from the multiplication // result in register A. This means that we must actually // divide the multiplication result in register A by 10. // We will exploit memory locations in the division process. // In the end, // - "first_digit_of_result" contains the division result // - the remainder will be left in "second_digit_of_result" set_memory_pointer second_digit_of_result store_register_a_to_memory set_memory_pointer first_digit_of_result load_register_a_with_value 0 store_register_a_to_memory divide_by_ten: set_memory_pointer second_digit_of_result load_register_a_from_memory load_register_b_with_value 10 jump_if_register_a_smaller_than_b division_ready subtract_register_b_from_a store_register_a_to_memory set_memory_pointer first_digit_of_result load_register_a_from_memory increment_register_a store_register_a_to_memory jump_to_address divide_by_ten division_ready: set_memory_pointer text_for_result_display call_subroutine display_text set_memory_pointer first_digit_of_result load_register_a_from_memory add_value_to_register_a '0' output_byte_from_register_a set_memory_pointer second_digit_of_result load_register_a_from_memory add_value_to_register_a '0' output_byte_from_register_a stop_processing read_and_echo_a_character: jump_if_input_not_ready read_and_echo_a_character input_byte_to_register_a output_byte_from_register_a return_to_calling_program display_text: load_register_a_from_memory jump_if_register_a_zero end_of_subroutine output_byte_from_register_a increment_memory_pointer jump_to_address display_text end_of_subroutine: return_to_calling_program text_for_number_input: STRING " * " text_for_result_display: STRING " = " multiplier: DATA 1 multiplicand: DATA 1 multiplication_result: DATA 1 first_digit_of_result: DATA 1 second_digit_of_result: DATA 1