Skip to content

Instantly share code, notes, and snippets.

@GabrielBdeC
Created May 20, 2021 21:27
Show Gist options
  • Save GabrielBdeC/676f8e7aa2d6d8595c7ab1df2ac952e0 to your computer and use it in GitHub Desktop.
Save GabrielBdeC/676f8e7aa2d6d8595c7ab1df2ac952e0 to your computer and use it in GitHub Desktop.
Assembly code for Intel 8085 that creates a randomic number based on the input
;y = (ax + b) % mod
;In portuguese: https://youtu.be/S0-osj9rNoY
.define
x_n 00H
q_n 01H
a_n 02H
b_n 03H
m_n 04H
d_0 77H
d_9 4Fh
f_o 07H
s_o 06H
t_o 05H
digit_addr 3FH ;Needs be 00XXH
store_ncounter 0049H
store_fd 004AH
store_sd 004BH
store_td 004CH
start_addr 004DH
TRAP_addr 0024H
RST_75_addr 003CH
RST_65_addr 0034H
RST_55_addr 002CH
store_memory_addr 1000H
.data digit_addr
DB 77H, 44H, 3EH, 6EH, 4DH, 6BH, 7BH, 46H, 7FH, 4FH
.org 0000H
CALL clearMemoryStore
CALL prepareOut
rep:
JMP rep
return:
RET
stop:
HLT
prepareOut:
CALL clearRegisters
CALL clearDigits
CALL resetShowNumber
RET
clearDigits:
PUSH PSW
MVI A, d_0
OUT f_o
OUT s_o
OUT t_o
POP PSW
RET
.org TRAP_addr
JMP showNext
.org RST_75_addr
JMP checkVar
.org RST_65_addr
RET
.org RST_55_addr
RET
.org start_addr
clearRegisters:
MVI A, 00H
LXI B, 0000H
LXI D, 0000H
LXI H, 0000H
RET
checkVar:
IN x_n
CPI 00H
JZ return
IN a_n
CPI 00H
JZ return
IN m_n
CPI 00H
JZ return
CALL clearRegisters
CALL clearMemoryStore
getQtd:
IN q_n
MOV D, A
CPI 00H
CZ counterm1
getSeed:
IN x_n
MOV L, A
CALL getMemoryStoreAddr
loopCalc:
CALL calc ;007F
DCR D
MOV A, L
STAX B
CPI 00H
JZ prepareOut
CALL incBC
MOV A, D
CPI 00H
JNZ loopCalc
JMP prepareOut
clearMemoryStore:
PUSH H
PUSH B
PUSH D
MVI E, FFH
LXI H, 0000H
CALL getMemoryStoreAddr
DAD B
LXI B, 0001H
CALL clearAddress
DAD B
clearMLoop:
CALL clearAddress
DAD B
DCR E
JNZ clearMLoop
clearMResult:
POP D
POP B
POP H
RET
clearAddress: ;addr(HL) = 0
PUSH PSW
PUSH B
MVI A, 00H
MOV B, H
MOV C, L
STAX B
POP B
POP PSW
RET
getMemoryStoreAddr: ;BC
LXI B, store_memory_addr
RET
counterm1:
INR D
RET
calc:
PUSH D
MOV D, L
IN a_n
MOV E, A
CALL mult
IN b_n
MOV E, A
DAD D
IN m_n
MOV E, A
CALL mod
POP D
RET
mult: ;HL = D*E
PUSH PSW
MOV A, D
MVI D, 00H
MVI H, 00H
MVI L, 00H
CPI 00H
iteration:
JZ endIteration
DAD D
DCR A
JMP iteration
endIteration:
POP PSW
RET
invert: ;E = FF - E + 1
PUSH PSW
MVI A, FFH
SUB E
INR A
MOV E, A
POP PSW
RET
sub21r: ;HL = HL - E
PUSH PSW
MOV A, l
CMP E
JP subL
subH:
CALL invert
DCR H
ADD E
MOV L, A
JMP subR
subL:
SUB E
MOV L, A
subR:
POP PSW
RET
mod: ;L = HL % E
PUSH PSW
modL:
MOV A, H
CPI 00H
JNZ modSub
MOV A, L
CMP E
JP modSub
JMP modR
modSub:
CALL sub21r
JMP modL
modR:
POP PSW
RET
incBC:
PUSH PSW
INR C
MOV A, C
CPI 00H
JNZ rerturnIncBC
INR B
rerturnIncBC:
POP PSW
RET
resetShowNumber:
PUSH PSW
MVI A, 00H
STA store_ncounter
CALL clearNumberShow
POP PSW
RET
clearNumberShow:
CALL setFirstDigit0
CALL setSecondDigit0
CALL setThirdDigit0
CALL clearDigits
RET
setFirstDigit0: ;addr(store_fd) = digit_addr
PUSH PSW
MVI A, digit_addr
OUT f_o
STA store_fd
POP PSW
RET
setSecondDigit0: ;addr(store_sd) = digit_addr
PUSH PSW
MVI A, digit_addr
OUT s_o
STA store_sd
POP PSW
RET
setThirdDigit0: ;addr(store_td) = digit_addr
PUSH PSW
MVI A, digit_addr
OUT t_o
STA store_td
POP PSW
RET
getNumberRandomized: ;A = addr(store_memory_addr + addr(store_ncounter))
PUSH B
PUSH H
LXI B, store_ncounter
LDAX B
LXI B, store_memory_addr
MOV L, C
MOV H, B
MVI B, 00H
MOV C, A
DAD B
MOV C, L
MOV B, H
LDAX B
POP B
POP H
RET
getFirstDigit: ;A = addr(addr(store_fd))
PUSH B
LXI B, store_fd
LDAX B
MOV C, A
LDAX B
POP B
RET
incFirstDigit: ;addr(store_fd) = addr(store_fd + 1)
PUSH PSW
PUSH B
LXI B, store_fd
LDAX B
INR A
STAX B
POP B
POP PSW
RET
getSecondDigit: ;A = addr(addr(store_fd))
PUSH B
LXI B, store_sd
LDAX B
MOV C, A
LDAX B
POP B
RET
incSecondDigit: ;addr(store_sd) = addr(store_sd + 1)
PUSH PSW
PUSH B
LXI B, store_sd
LDAX B
INR A
STAX B
POP B
POP PSW
RET
getThirdDigit: ;A = addr(addr(store_td))
PUSH B
LXI B, store_td
LDAX B
MOV C, A
LDAX B
POP B
RET
incThirdDigit: ;addr(store_td) = addr(store_td + 1)
PUSH PSW
PUSH B
LXI B, store_td
LDAX B
INR A
STAX B
POP B
POP PSW
RET
showNext:
PUSH PSW
PUSH D
CALL getNumberRandomized
CPI 00H
JZ resetShowNumber
MOV E, A
CALL clearNumberShow
showNextLoop:
CALL getFirstDigit
CPI d_9
JZ secondDigitNext
CALL incFirstDigit
CALL getFirstDigit
OUT f_o
verifyLoopShowNext:
DCR E
JNZ showNextLoop
CALL incCounter
POP D
POP PSW
RET
secondDigitNext:
CALL setFirstDigit0
CALL getSecondDigit
CPI d_9
JZ thirdDigitNext
CALL incSecondDigit
CALL getSecondDigit
OUT s_o
JMP verifyLoopShowNext
thirdDigitNext:
CALL setSecondDigit0
CALL incThirdDigit
CALL getThirdDigit
OUT t_o
JMP verifyLoopShowNext
incCounter: ;addr(store_ncounter) = addr(store_ncounter + 1)
PUSH PSW
PUSH B
LXI B, store_ncounter
LDAX B
INR A
STAX B
POP B
POP PSW
RET
@GabrielBdeC
Copy link
Author

GabrielBdeC commented May 20, 2021

The inputs needs to be use in the IN ports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment