这篇文章记录一下在学习CSE 230 - Computer Org/Assemb Lang Prog 这门课写地第一个汇编程序小作业。

作业要求

最后打印出来要是这个效果:

Enter c3? 37 Enter c2? -148 Enter c1? 120 Enter c0? -47 Enter x? -13
p(x) = 37x^3 + -148x^2 + 120x + -47 at x = -13 is -107908

简单来说就是需要让用户输入c3,c2,c1,c0,x这四个数值(这四个数一定是整数Integer),按照p(x) = c3x^3 + c2x^2 + c1x + x这个公式计算最终的数值,并且按照要求打印出最后一行。

我的代码

毕竟是第一次写汇编,可能代码很拙劣,见谅。

#******************************************************************
# FILE: poly3.s
#
# DESCRIPTION
# calculat p(x)
#
# COURSE INFORMATION
# CSE230, Summer 2022, Homework Assignment 1, Exercise 7 #
# AUTHOR
# 
#******************************************************************

.data
    promptc3: .asciiz " Enter c3? "
    promptc2: .asciiz " Enter c2? "
    promptc1: .asciiz " Enter c1? "
    promptc0: .asciiz " Enter c0? "
    promptx: .asciiz " Enter x? "
    message00: .asciiz "p(x) = "
    message01: .asciiz "x^3"
    message02: .asciiz "x^2"
    message03: .asciiz "x"
    message04: .asciiz " + "
    message05: .asciiz " at x = "
    message06: .asciiz " is "
 .text
    # prompt the user to enter c3
    li $v0, 4
    la $a0, promptc3
    syscall
    # get c3
    li $v0, 5
    syscall
    # Store the result in $t0
    move $t0, $v0

    # prompt the user to enter c2
    li $v0, 4
    la $a0, promptc2
    syscall
    # get c2
    li $v0, 5
    syscall
    # Store the result in $t1
    move $t1, $v0

    # prompt the user to enter c1
    li $v0, 4
    la $a0, promptc1
    syscall
    # get c1
    li $v0, 5
    syscall
    # Store the result in $t2
    move $t2, $v0

    # prompt the user to enter c0
    li $v0, 4
    la $a0, promptc0
    syscall
    # get c1
    li $v0, 5
    syscall
    # Store the result in $t2
    move $t3, $v0

    # prompt the user to enter x
    li $v0, 4
    la $a0, promptx
    syscall
    # get x
    li $v0, 5
    syscall
    # Store the result in $t4
    move $t4, $v0

    ## Now we got all values that we need for calculation
    ## c3 -> $t0, c2 -> $t1, c1 -> $t2
    ## c0 -> $t3, x -> $t4

    # calculat the value c3x^3, store it to $a0
    mul $a0, $t4, $t4
    mul $a0, $a0, $t4
    mul $a0, $a0, $t0

    # calculat the value c2x^2, store it to $a1
    mul $a1, $t4, $t4
    mul $a1, $a1, $t1

    # calculat the value c1x, store it to $a2
    mul $a2, $t2, $t4

    # calculat p(x), adding c3x^3 + c2x^2 + c1x + c0, store it to $a3
    add $a3, $a0, $a1
    add $a3, $a3, $a2
    add $a3, $a3, $t3

    # Print or show the answer
    li $v0, 4
    la $a0, message00
    syscall
    li $v0, 1
    move $a0, $t0
    syscall
    li $v0, 4
    la $a0, message01
    syscall
    li $v0, 4
    la $a0, message04
    syscall
    li $v0, 1
    move $a0, $t1
    syscall
    li $v0, 4
    la $a0, message02
    syscall
    li $v0, 4
    la $a0, message04
    syscall
    li $v0, 1
    move $a0, $t2
    syscall
    li $v0, 4
    la $a0, message03
    syscall
    li $v0, 4
    la $a0, message04
    syscall
    li $v0, 1
    move $a0, $t3
    syscall
    li $v0, 4
    la $a0, message05
    syscall
    li $v0, 1
    move $a0, $t4
    syscall
    li $v0, 4
    la $a0, message06
    syscall
    li $v0, 1
    move $a0, $a3
    syscall

解释

MIPS32架构有32个通用寄存器,总共有12种。如下表:

英文版:

NameNumberDescription
$zero0constant value 0
$at1assembler temp
$v0 $v12-3function return
$a0~$a34-7argument
$t0~$t78-15temporary value
$s0~$s716-23saved temporary
$t8~$t924-25temporary value
$k0 $k126-27reserved for OS
$gp28global pointer
$sp29stack pointer
$fp30frame pointer
$ra31return address

中文版:

名称编号描述
$zero0始终为0,作为常量寄存器
$at1保留,向上兼容
$v0 $v12-3函数返回值寄存器
$a0~$a34-7函数前四个参数
$t0~$t78-15临时变量寄存器
$s0~$s716-23帧变量寄存器,函数返回时必恢复
$t8~$t924-25临时变量寄存器
$k0 $k126-27中断寄存器
$gp28全局指针寄存器,静态变量指针寄存器
$sp29栈顶指针寄存器
$fp30栈帧寄存器
$ra31返回地址寄存器

因为我们一共只需要存5个变量,因此我们可以把收集到的整数全部存在$t0~$t7就行。如果超过寄存器可容纳的数量,那就得把数据存到内存中,需要时再从内存里取出来。

.data是声明变量的地方(这些数据都在内存中), .text是写操作指令(Instruction)的地方。由于我们需要打印出字符串,因此我们申明了几个字符串的变量。