Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

DailyProgrammer Challenge 121 Dynamic Programming solution in S390 assembly

View gist:5094380
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
.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
Something went wrong with that request. Please try again.