Skip to content

Instantly share code, notes, and snippets.

@russross
Created January 17, 2017 17:34
Show Gist options
  • Save russross/ad2859480a2fa717f65fb2303446f1eb to your computer and use it in GitHub Desktop.
Save russross/ad2859480a2fa717f65fb2303446f1eb to your computer and use it in GitHub Desktop.
ARMv6 assembly solution to a math puzzle
@ ARMv6 solution to the puzzle:
@
@ 13 JASONs when added equal FRIDAY.
@ Use only the digits 0 to 9.
@ No leading zeros. Each letter maps to one and
@ only one number.
@
@ JASON + JASON +
@ JASON + JASON + JASON +
@ JASON + JASON + JASON +
@ JASON + JASON + JASON +
@ JASON + JASON = FRIDAY
@
@ This solution works by cycling through all possible values for JASON
@ starting at 10,000 and the matching values of FRIDAY
@ starting at 130,000.
@
@ It tracks each base 10 digit in its own register. For each
@ iteration, it adds 1 to JASON and 13 to FRIDAY, then
@ checks for a winner.
@
@ Compile on a Raspberri Pi running Raspbian using:
@ as -g jasonfriday.s -o jasonfriday.o
@ ld jasonfriday.o -o jasonfriday
@
@ Russ Ross (github user russross), January 17, 2017
.global _start
.equ sys_exit, 1
.equ sys_write, 4
.equ stdout, 1
.text
_start:
@ r0-r10: decimal digits of JASONFRIDAY
mov r0, #1 @ J
mov r1, #0 @ A
mov r2, #0 @ S
mov r3, #0 @ O
mov r4, #0 @ N
mov r5, #1 @ F
mov r6, #3 @ R
mov r7, #0 @ I
mov r8, #0 @ D
mov r9, #0 @ A
mov r10, #0 @ Y
@ r11: used to test if every digit is used once
@ r12: 1
@ r14: 0b1111111111
mov r12, #1
ldr r14, =0b1111111111
1:
@ if the two A slots do not match, continue
cmp r1, r9
bne 2f
@ if any other digits are repeated, continue
mov r12, #1
mov r11, r12, lsl r0
orr r11, r11, r12, lsl r1
orr r11, r11, r12, lsl r2
orr r11, r11, r12, lsl r3
orr r11, r11, r12, lsl r4
orr r11, r11, r12, lsl r5
orr r11, r11, r12, lsl r6
orr r11, r11, r12, lsl r7
orr r11, r11, r12, lsl r8
orr r11, r11, r12, lsl r10
cmp r11, r14
bne 2f
@ we have a winner!
@ this only happens a few times,
@ so do it the easy way and push everything
push {r0-r12, r14}
bl report
pop {r0-r12, r14}
2:
@ increment JASON by one
add r4, r4, #1 @ N
cmp r4, #10
blt 3f
sub r4, r4, #10
add r3, r3, #1 @ O
cmp r3, #10
blt 3f
sub r3, r3, #10
add r2, r2, #1 @ S
cmp r2, #10
blt 3f
sub r2, r2, #10
add r1, r1, #1 @ A
cmp r1, #10
blt 3f
sub r1, r1, #10
@ FRIDAY will overflow before JASON
add r0, r0, #1 @ J
3:
@ increment FRIDAY by 13
@ add the 3 to Y first
add r10, r10, #3 @ Y
cmp r10, #10
subge r10, r10, #10
@ add the carried 1 if needed
addge r9, r9, #1 @ A
@ always add the 1 from 13
add r9, r9, #1
cmp r9, #10
blt 1b
sub r9, r9, #10
add r8, r8, #1 @ D
cmp r8, #10
blt 1b
sub r8, r8, #10
add r7, r7, #1 @ I
cmp r7, #10
blt 1b
sub r7, r7, #10
add r6, r6, #1 @ R
cmp r6, #10
blt 1b
sub r6, r6, #10
add r5, r5, #1 @ F
cmp r5, #10
blt 1b
@ FRIDAY overflowed, so we are finished
mov r0, #0
mov r7, #sys_exit
svc #0
report:
ldr r12, =jason
add r0, r0, #'0'
strb r0, [r12]
add r1, r1, #'0'
strb r1, [r12,#1]
add r2, r2, #'0'
strb r2, [r12,#2]
add r3, r3, #'0'
strb r3, [r12,#3]
add r4, r4, #'0'
strb r4, [r12,#4]
ldr r12, =friday
add r5, r5, #'0'
strb r5, [r12]
add r6, r6, #'0'
strb r6, [r12,#1]
add r7, r7, #'0'
strb r7, [r12,#2]
add r8, r8, #'0'
strb r8, [r12,#3]
add r9, r9, #'0'
strb r9, [r12,#4]
add r10, r10, #'0'
strb r10, [r12,#5]
mov r0, #stdout
ldr r1, =buffer
mov r2, #len
mov r7, #sys_write
svc #0
bx lr
.data
buffer: .ascii "JASON = "
jason: .space 5
.ascii ", FRIDAY = "
friday: .space 6
.ascii "\n"
.equ len, .-buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment