Skip to content

Instantly share code, notes, and snippets.

@ReaperUnreal
Last active December 14, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReaperUnreal/5094380 to your computer and use it in GitHub Desktop.
Save ReaperUnreal/5094380 to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge 121 Dynamic Programming solution in S390 assembly
.section .text
.align 4
.globl foo
.type foo,@function
foo:
.foo:
STG GPR14,112(,GPR15)
STMG GPR7,GPR11,56(GPR15)
LA GPR1,0(,GPR15)
LAY GPR15,-160(,GPR15)
STG GPR1,-160(,GPR1)
LGFR GPR2,GPR2
#calculate amount of room needed
LA GPR3,1(,GPR2)
SLLG GPR7,GPR3,3(GPR0) #8 byte slots
#fake alloca, a real one would have an MVC to move the stack
LNGR GPR7,GPR7 #negate
LA GPR11,0(GPR7,GPR15) #new stack pointer
#store initial data
LGR GPR9,GPR3 #size of array
LA GPR7,160(,GPR11) #array pointer
LA GPR4,1(,GPR0)
STG GPR4,0(GPR7)
#do the computation in DP fashion, index = R4, max = R9, accume = R8, working regs = R2,R3,R5
_compute:
#load i/2
#shifts can be replaced with RISBG on newer machines
SRLG GPR3,GPR4,1(GPR0)
SLLG GPR3,GPR3,3(GPR0)
LG GPR8,0(GPR3,GPR7)
#load i/3
LGR GPR3,GPR4
XGR GPR2,GPR2
LA GPR5,3(,GPR0)
DLGR GPR2,GPR5 # GPR2:GPR3/GPR5 = GPR3 R GPR2
SLLG GPR3,GPR3,3(GPR0)
ALG GPR8,0(GPR3,GPR7)
#load i/4
SRLG GPR3,GPR4,2(GPR0)
SLLG GPR3,GPR3,3(GPR0)
ALG GPR8,0(GPR3,GPR7)
#store sum at index
SLLG GPR3,GPR4,3(GPR0)
STG GPR8,0(GPR3,GPR7)
#increment, compare, and branch
LA GPR4,1(,GPR4)
BRCT GPR9,_compute #subtract 1 then compare with 0, if equal don't branch
#load final answer
LG GPR2,152(,GPR15)
_end:
LMG GPR7,GPR11,216(GPR15)
LG GPR14,272(,GPR15)
LA GPR15,160(,GPR15)
BCR 0xf,GPR14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment