Last active
July 8, 2018 10:47
-
-
Save klauspost/bdf58d25565d3f6d5d13019e16529a6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Finds the rounded down square root of 8 bit value value in 14. | |
# Uses self-modifying code to store result, so | |
# instruction 11 (LDI 0) and the two values (14, 15) needs to be reset between runs. | |
# | |
LDA 14 | |
SUB 15 | |
JPC 11 | |
STA 14 | |
LDI 1 | |
ADD 11 | |
STA 11 | |
LDI 2 | |
ADD 15 | |
STA 15 | |
JMP 0 | |
LDI 0 | |
OUT 0 | |
HLT 0 | |
14: 143 | |
15: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
00011110 | |
00111111 | |
01111011 | |
01001110 | |
01010001 | |
00101011 | |
01001011 | |
01010010 | |
00101111 | |
01001111 | |
01100000 | |
01010000 | |
11100000 | |
11110000 | |
1110: 10001111 | |
1111: 00000001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func main() { | |
for i := 0; i < 256; i++ { | |
ref := math.Sqrt(float64(i)) | |
fmt.Println(i, sqrt(uint8(i)), uint8(ref)) | |
} | |
} | |
func sqrt(x uint8) uint8 { | |
r1 := x // input | |
r2 := uint8(1) // number to subtract | |
r3 := uint8(0) // result | |
for { | |
// We don't have JC, so this will have to do. | |
if r2 > r1 { | |
return r3 | |
} | |
r1 = r1 - r2 | |
r2 += 2 | |
r3++ | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/k2o8_U_OpSj