assembly - 不使用分支指令的 n 个自然数(1 到 n)的 MIPS 代码总和
问题描述
.data
input: .asciiz "Enter limit for the sum of natural number = "
show: .asciiz " \n sum of natural numbers are = "
.text
main:
#get the integer input from user
li $v0, 4
la $a0, input
syscall
li $v0, 5
syscall
move $s5, $v0
li $v0, 1
move $a0, $s5
syscall
addi $t5, $t5, 0
iteration:
blt $t5, $s5, increment
j end
increment:
add $s2, $s2, $t5
addi $t5, $t5, 1
j iteration
end:
li $v0, 4
la $a0, show
syscall
li $v0, 1
move $a0, $s2
syscall
好的,所以我知道如何通过用户输入使用分支指令来获取 n 个自然数的总和,但我的问题是:“如何在不使用分支指令(bge、bltv bne 等)的情况下执行相同的程序”
解决方案
1..n 之和等于 (n*(n+1))/2
在伪代码中:
temp := (input + 1)
temp := (temp * input)
result := (temp >> 1)
推荐阅读
- r - 创建一个函数并使用循环计算累积日期和id
- amazon-s3 - Snowflake-S3:将两种类型的文件(来自同一个 S3 存储桶)摄取到两个不同的表中
- python - IMG2PY 用于 TKINTER 中的背景标签和图标
- javascript - 如何使用 CSS hidden 属性重定向到新页面?
- python - 从 DataFrame 中删除具有多列和字符串的连续重复项
- c - 在 C 中使用正则表达式进行模式匹配
- excel - VBA - 组合表以添加唯一行
- python - 如何多次使用同一个 mp3 文件?
- javascript - 给定一个 EventEmitters 数组,只监听第一个事件
- javascript - 通过 HTTP 请求发送时,Javascript 变量的输出为空