Skip to content

Instantly share code, notes, and snippets.

@mjdargen
Last active July 7, 2021 20:46
Show Gist options
  • Save mjdargen/78ab6c0ed306ad3f3567e7de7b6c72e2 to your computer and use it in GitHub Desktop.
Save mjdargen/78ab6c0ed306ad3f3567e7de7b6c72e2 to your computer and use it in GitHub Desktop.
Starting point for PS6 Question #3.
;
; calc_v2.asm
; Performs addition/subtraction/multiplication/division/modulus/exponentiation
; between 2 single-digit, user-entered values to produce 1-2 digit result.
;
; R5 - operation type
; R4 - operand 1
; R3 - operand 2
.ORIG x3000
; retrieve operand 1 value from user - convert to integer value
GETC ; retrieve character entered
OUT ; echo it back to user
ADD R4, R0, #0 ; move value from R0 to R4
LD R1, ZERO ; load ASCII Base for zero
NOT R1, R1
ADD R1, R1, #1 ; negate ASCII base
ADD R4, R4, R1 ; add negated base to produce numerical value
; output a space
LD R0, SP ; load a Space
OUT ; output a Space
; retrieve operation
GETC ; retrieve character entered
OUT ; echo it back to user
ADD R5, R0, #0 ; move value from R0 to R5
; output a space
LD R0, SP ; load a Space
OUT ; output a Space
; retrieve operand value from user - convert to integer value
GETC ; retrieve character entered
OUT ; echo it back to user
ADD R3, R0, #0 ; move value from R0 to R3
ADD R3, R3, R1 ; add negated base to produce numerical value
; output a space
LD R0, SP ; load a Space
OUT ; output a Space
; check operation types
; check for subtraction
LD R6, MINUS
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz SUBIT
; check for addition
LD R6, PLUS
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz ADDIT
; check for multiply
LD R6, STAR
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz MULIT
; check for divide
LD R6, SLASH
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz DIVIT
; check for modulo
LD R6, PERC
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz MODIT
; check for exponentiation
LD R6, POW
NOT R6, R6,
ADD R6, R6, #1 ; negate
ADD R6, R5, R6 ; subtract to compare
BRz EXPIT
ADDIT JSR ADDITION
BRnzp END
SUBIT JSR SUBTRACT
BRnzp END
MULIT JSR MULTIPLY
BRnzp END
DIVIT JSR DIVIDE
BRnzp END
MODIT JSR MODULUS
BRnzp END
EXPIT JSR EXPONENT
BRnzp END
END JSR INT2CHR
HALT
; --------------------------- ADDITION SUBROUTINE ---------------------------- ;
; Description: performs addition between two values - R1 = R4 + R3
; Inputs: R4 - value 1, R3 - value 2
; Outputs: R1 - sum
ADDITION
RET
; ------------------------- SUBTRACTION SUBROUTINE -------------------------- ;
; Description: performs subtraction between two values - R1 = R4 - R3
; Inputs: R4 - minuend, R3 - subtrahend
; Outputs: R1 - difference
SUBTRACT
RET
; ------------------------ MULTIPLICATION SUBROUTINE ------------------------- ;
; Description: performs multiplication between two values - R1 = R4 * R3
; Inputs: R4 - factor 1, R3 - factor 2
; Outputs: R1 - product
MULTIPLY
RET
; --------------------------- DIVISION SUBROUTINE ---------------------------- ;
; Description: performs division between two values - R1 = R4 / R3
; Inputs: R4 - dividend, R3 - divisor
; Outputs: R1 - quotient
DIVIDE RET
; --------------------------- MODULUS SUBROUTINE ---------------------------- ;
; Description: performs modulus between two values - R1 = R4 % R3
; Inputs: R4 - dividend, R3 - divisor
; Outputs: R1 - remainder
MODULUS
RET
; ------------------------ EXPONENTIATION SUBROUTINE ------------------------- ;
; Description: performs exponentiation between two values - R1 = R4 ^ R3
; Inputs: R4 - value 1, R3 - value 2
; Outputs: R1 - result
EXPONENT
RET
; ----------------------- INTEGER TO ASCII SUBROUTINE ------------------------ ;
; Description: converts 1-2 digit integer to ASCII char and prints to console.
; Input: R1 - integer value to convert
; Output: No registers. Prints resulting ASCII characters to console.
INT2CHR
RET
; ----------------------- RAW VALUES STORED IN MEMORY ------------------------ ;
EQUALS .FILL x003D ; ASCII representation of an equals sign '='
PLUS .FILL x002B ; ASCII representation of a plus sign '+'
MINUS .FILL x002D ; ASCII representation of a minus sign '-'
STAR .FILL x002A ; ASCII representation of a minus sign '*'
SLASH .FILL x002F ; ASCII representation of a minus sign '/'
PERC .FILL x0025 ; ASCII representation of a minus sign '%'
POW .FILL x005E ; ASCII representation of a minus sign '^'
SP .FILL x0020 ; ASCII representation of a space ' '
ZERO .FILL x0030 ; ASCII offset for ASCII->Integer conversions
LF .FILL x000A ; ASCII representation of linefeed '\n'
.END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment