# a in 0(sp), b in 4(sp), c in 12(sp), d in 52(sp)addisp,sp,-56lit0,5swt0.0(sp)# a = 5lit0,0x69727473swt0,4(sp)lit0,0x0000676Eswt0,8(sp)# b = "string"lbt0,7(sp)sbt0,52(sp)# d = b[3]lwt0,0(sp)lbut1,52(sp)addt2,t0,t1swt2,28(sp)# c[4] = a + dlit0,20lwt1,0(sp)sllit1,t1,2# a * 4addit1,t1,12addt1,t1,spswt0,0(t1)# c[a] = 20
# sb using sw: sb s1, 3(s0)lwt0,0(s0)# load current wordlit1,0x00FFFFFF# load bitmaskandt0,t0,t1# zero top bytesllit1,s1,24# little-endian & shift leftort0,t0,t1# combinesws1,0(s0)# store
# copy String p to q# p -> s0, q -> s1loop:lbt0,0(s0)# load bytesbt0,0(s1)# store byteaddis0,s0,1# increment paddis1,s1,1# increment qbnet0,x0,loop# loop if not nulljexit# exit if null
Prologuefunc_label:addisp,sp,-framesizeswra,(framesize-4)(sp)# store other callee-saved registers# save other regs if neededBody# function bodyEpilogue# restore other regs if needed# restore callee-saved registerslwra,(framesize-4)(sp)addisp,sp,framesizejrra# or ret
fibonacci:lit0,2# test if n < 2 blta0,t0,fib_base# if n < 2, return 1addisp,sp,-8# allocate stack spaceswra,4(sp)# store return addressswa0,0(sp)# store original naddia0,a0,-1# n-1 in a0jalx1,fibonacci# calculate fib(n-1)lwt0,0(sp)# load original n to t0swa0,0(sp)# store fib(n-1) to stackaddia0,t0,-2# now n-2 in a0jalx1,fibonacci# calculate fib(n-2)lwt0,0(sp)# load fib(n-1) to t0adda0,a0,t0# calculate fib(n) = fib(n-1) + fib(n-2)lwra,4(sp)# load return addressaddisp,sp,8# clean up stackretfib_base:# base case, return 1lia0,1ret