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"

Dani AI

Generated

A minimal conceptual change is required: make the per-iteration term equal to the operand cubed rather than squared. already pointed in the right direction — perform the extra multiplication (use the original operand once more) before adding the term to the running total. That extra multiply must occur immediately after the square is computed and before the accumulator is updated.

Two practical pitfalls to watch for. First, preserve the original operand across the extra multiply: if the squaring step overwrites the operand register, save it in a different temporary (or recompute the operand into a spare register) so the second multiplication uses the correct value. Second, assembler differences matter: some MIPS toolchains expose a single-instruction multiply while others require the mult/mflo sequence; check the target assembler/ABI so the multiply you insert is legal and fetches the expected 32‑bit/64‑bit product (MIPS instruction set).

A quick sanity check: use the closed-form identity for verification. The sum of cubes from 1 to n equals (n(n+1)/2)^2, so for n=100 the result is 5050^2 = 25,502,500. That gives a simple correctness check for the modified loop and tells whether the accumulator width is sufficient; larger n will need 64‑bit accumulation to avoid overflow (Faulhaber’s formula).

In 's snippet the loop and printing logic can remain unchanged; only the term calculation needs the extra multiply and care about register preservation and instruction choice.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.