0

How do I change the following code to be from a sum of squares to 100 to a sum of cubes? If you can help will you please point out the line(s)? Thanks.

# FILE = figA4.s
# From jws@cs.uga.edu Tue Oct  7 08:46 EDT 2003

	.text
	.align	2
	.globl	main
main:
	subu	$sp,$sp,32
	sw	$ra,20($sp)
	sd	$a0,32($sp)
	sw	$0,24($sp)
	sw	$0,28($sp)
loop:
	lw	$t6,28($sp)
	mul	$t7,$t6,$t6
	lw	$t8,24($sp)
	addu	$t9,$t8,$t7
	sw	$t9,24($sp)
	addu	$t0,$t6,1
	sw	$t0,28($sp)
	ble	$t0,100,loop
#	la	$a0,str
#	lw	$a1,24($sp)
#	jal	printf       # we dont have printf, use next 6 instrs:
        la      $a0,str      # 1
        li      $v0,4        # 2
        syscall              # 3
        lw      $a0,24($sp)  # 4
        li      $v0,1        # 5
        syscall              # 6
	move	$v0,$0
	lw	$ra,20($sp)
	addu	$sp,$sp,32
	j	$ra

	.data
	.align	0
str:
	.asciiz	"The sum of the cubes from 1 to 100 is %d\n"
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Greywolf333
0

How do I change the following code to be from a sum of squares to 100 to a sum of cubes? If you can help will you please point out the line(s)? Thanks.

# FILE = figA4.s
# From jws@cs.uga.edu Tue Oct  7 08:46 EDT 2003

	.text
	.align	2
	.globl	main
main:
	subu	$sp,$sp,32
	sw	$ra,20($sp)
	sd	$a0,32($sp)
	sw	$0,24($sp)
	sw	$0,28($sp)
loop:
	lw	$t6,28($sp)
	mul	$t7,$t6,$t6
	lw	$t8,24($sp)
	addu	$t9,$t8,$t7
	sw	$t9,24($sp)
	addu	$t0,$t6,1
	sw	$t0,28($sp)
	ble	$t0,100,loop
#	la	$a0,str
#	lw	$a1,24($sp)
#	jal	printf       # we dont have printf, use next 6 instrs:
        la      $a0,str      # 1
        li      $v0,4        # 2
        syscall              # 3
        lw      $a0,24($sp)  # 4
        li      $v0,1        # 5
        syscall              # 6
	move	$v0,$0
	lw	$ra,20($sp)
	addu	$sp,$sp,32
	j	$ra

	.data
	.align	0
str:
	.asciiz	"The sum of the cubes from 1 to 100 is %d\n"

Let me write out the operation (mostly for my own benefit)...

main: 24sp initialized to 0. 28sp also initialized to 0.

loop:
t6 now has 0, from 28sp
t7 now has 0, from t6*t6
t8 now has 0, from 24sp
t9 now has 0, from t8+t7
24sp now has 0, from t9
t0 now has 1, from t6+1
28sp now has 1, from t0
ble <= 100 since t0 = 1, so loop.


OK so t6 is the operand. The number to be squared.
t7 holds the square.
t8 holds the old sum from the last iteration.
t9 calculates the new sum by adding t7 and t8.
this new sum is put into 24sp so it can be read NEXT iteration as the old sum.
the new operand that t6 is put into sp28 for use for NEXT iteration, by adding t6 and 1.

It looks like you could accomplish a sum of cubes by simply adding this line after the "mul" line:

mul $t7, $t7, $t6

essentially from the original line, t7 might have 3*3, or 9.
now it will have 9*3.

-Greywolf

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.