69 lines
1.4 KiB
NASM
69 lines
1.4 KiB
NASM
|
|
// This file is part of www.nand2tetris.org
|
|
// and the book "The Elements of Computing Systems"
|
|
// by Nisan and Schocken, MIT Press.
|
|
// File name: projects/6/max/Max.asm
|
|
|
|
// Computes R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2])
|
|
// Usage: Before executing, put two values in R0 and R1.
|
|
|
|
|
|
// D = R0 - R1
|
|
|
|
/*
|
|
@R0
|
|
D=M
|
|
@R1
|
|
D=D-M
|
|
// If (D > 0) goto ITSR0
|
|
@ITSR0
|
|
D;JGT
|
|
// Its R1
|
|
@R1
|
|
D=M
|
|
@OUTPUT_D
|
|
0;JMP
|
|
(ITSR0)
|
|
@R0
|
|
D=M
|
|
(OUTPUT_D)
|
|
@R2
|
|
M=D
|
|
(END)
|
|
@END
|
|
0;JMP
|
|
|
|
*/
|
|
|
|
// Max
|
|
// Takes two numbers and saves the highest of them
|
|
// Get the sum of R0 & R1
|
|
// Save it in R2
|
|
// Get Value of R0
|
|
// Compare it value of R1
|
|
// Remember that the conditional can only be compared to zero.
|
|
// GT. LS, EQ etc.. All to zero
|
|
// Create a label and jump to it
|
|
|
|
@R0
|
|
D=M // Value of R0
|
|
@R1
|
|
D=D-M // New D(playground) is Old D(Value of R0) Minus Value of R1. Basically R0 - R1
|
|
@ITSR0
|
|
D;JGT // If D (R0 - R1) is greater than 0, then R0 is biger. Otherwise R1 or equal.
|
|
// Else it R1
|
|
@R1 // Get The Value of R1
|
|
D=M
|
|
@OUTPUT_D // And send it to the output section which is R2 variable
|
|
0;JMP
|
|
// The end Statements
|
|
(ITSR0) // Jump here if R0
|
|
@R0
|
|
D=M // Set value of R0 to the playground variable.
|
|
(OUTPUT_D) // End up here either way. Depending on value.
|
|
@R2
|
|
M=D // Set R2 to D either way. Just with or without the ITSR0 iteration
|
|
(END) // Infinite loop - prevents CPU from executing undefined instructions.
|
|
@END
|
|
0;JMP // Infinite Jumping to end the program
|