; ;
; HW #3 ; ; ; hw3.asm ; *************** Comments *************** ; This LC2 assembler program performs the following operations: ; 1) input a character ; 2) convert to integer ; 3) input a character ; 4) convert to integer ; 5) adds the integers ; 6) convert the result back to ASCII ; 7) output the result ; Shortcoming: any result above 10 doesn't quite give the right answer. ; Remember, $ = hex, # = decimal, % = binary ; ****************************************** .ORIG $3000 ; directive: put code at start of user memory ; Get the character inputs; the OS provides the prompts for each character ; input IN ; read character into R0 ADD R1, R0, #0 ; move R0 into R1 IN ; read character into R0 ; subtract the value of 0 in ASCII to get R0 and R1 into 'real' numerical form LD R3, ASCIIZERO ; compute the negative of ASCIIZERO so we can use add to subtract NOT R3, R3 ; invert ADD R3, R3, #1 ; add 1 ADD R1, R1, R3 ; subtract ASCIIZERO from R1 ADD R0, R0, R3 ; subtract ASCIIZERO from R0 ; do the addition ADD R0, R0, R1 ; add numbers ; compute the ASCII value of the result LD R3, ASCIIZERO ADD R0, R0, R3 ; so 2 + 3 = 5, but 8 + 9 = 'A'; Oh well! ; write the results ADD R1, R0, #0 ; move R0 into R1 LD R0, ASCIINewLine OUT ; write newline character to screen LEA R0, ResultStr trap #34 ; write string out using puts ADD R0, R1, #0 ; move R1 back to R0 OUT ; write out the single-character result in R0 LD R0, ASCIINewLine OUT ; write newline character to screen HALT ; ********* DATA ********** ASCIIZERO .FILL $30 ASCIINewLine .FILL $000A ;an ASCII newline character ResultStr .FILL "The answer is " .END ; directive: no more code