如何完成这项组装作业
问题:完成以下组装代码,以计算商的值和剩余的表达式(AX2 -BX)/(CX + 1),其中a,b,c被给予,而x将是键盘的输入?
示例:
input the value of x: 3
(a*x^2 - b*x)/(c*x + 1) = 1
remainder = 5
解决方案:
.data
a: .word 3
b: .word 5
c: .word 2
prompt: .asciiz "input the value of x: "
quotient: .asciiz "(a*x^2 - b*x)/(c*x + 1) = "
remainder: .asciiz "remainder = "
.text
main:
lw $t0, a
lw $t1, b
lw $t2, c
li $t4, 1 # load the constant 1 from the expression (a*x^2 - b*x)/(c*x + 1) to the register $t4
la $a0, prompt
jal printString
...
...
...
printString:
li $v0, 4
syscall
jr $ra
printInt:
li $v0, 1
syscall
jr $ra
newLine:
li $v0, 11
....
...
...
Question: Complete the following assembly code to calculate the value of the quotient and the remainder of the expression (ax2 - bx)/(cx + 1), where a, b, c are given and x will be your input from the keyboard?
Example:
input the value of x: 3
(a*x^2 - b*x)/(c*x + 1) = 1
remainder = 5
Solution:
.data
a: .word 3
b: .word 5
c: .word 2
prompt: .asciiz "input the value of x: "
quotient: .asciiz "(a*x^2 - b*x)/(c*x + 1) = "
remainder: .asciiz "remainder = "
.text
main:
lw $t0, a
lw $t1, b
lw $t2, c
li $t4, 1 # load the constant 1 from the expression (a*x^2 - b*x)/(c*x + 1) to the register $t4
la $a0, prompt
jal printString
...
...
...
printString:
li $v0, 4
syscall
jr $ra
printInt:
li $v0, 1
syscall
jr $ra
newLine:
li $v0, 11
....
...
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在问如何将复杂的表达式转化为汇编语言。
基本方法是将问题分解为一组简单的问题,然后解决更简单的问题,然后将更简单问题的解决方案重新组装为整体解决方案。
首先,让我们查看三个地址代码的概念。
三个地址代码将表达式分解为基本部分,因此我们每行执行一个操作:一个操作,例如加法或乘法。
而不是
a+b*c
,在TAC中,我们具有这种表达式的分解,需要引入临时变量,该变量将TAC的行互连。
在组装语言中,我们可以用CPU寄存器代替临时变量。
我们还要注意一种称为
如果我们需要对某物进行平衡,那么大多数处理器就不会作为整数操作,因此,如果我们有:
我们可以降低算术操作的强度,因为知道操作数之一是恒定的。
产生相同的答案,因此等同于平方。
像MIPS这样的硬件不能进行整数正方形,但可以进行乘法。
(更简单的硬件(例如LC-3或Marie)甚至无法直接进行乘法,因此甚至可以将
2*X
还可以简化为X+X
,然后在此类简单的硬件上完成。)You're asking how to go about translating a complex expression into assembly language.
The basic approach is to decompose the problem into a set of simpler problems, then solve the simpler problems, then reassembly the solution to the simpler problems into an overall solution.
Let's start by looking at the concept of three address code.
Three address code breaks an expression into fundamental parts, so that we do one operation per line: one operation like addition or multiplication.
Instead of
a+b*c
, in TAC we haveThis decomposition of expressions requires the introduction of temporary variables, which interconnect the lines of TAC.
In assembly language we can substitute temporary variables with CPU registers.
Let's also note an approach called Strength Reduction.
If we need to square something, most processors won't have that as an integer operation, so if we have TAC of:
we can reduce the strength of the arithmetic operation, knowing that one of the operands is a constant.
produces the same answer and so is equivalent to squaring.
Hardware like MIPS cannot do integer square but can do multiplication.
(Simpler hardware like LC-3 or MARIE cannot even do multiplication directly so even
2*x
can be reduced tox+x
and then done on such simpler hardware.)