0

将华氏温度值转换为摄氏温度的汇编语言程序。要实现的公式是 = ( − 32) × 5⁄9。所需的数据段:

  1. F_temp(字)
  2. C_temp(字)
  3. 值 32(字节)
  4. 值 5(字节)
  5. 值 9(字节)
  6. 提示输入(字符串)
  7. 输出消息(字符串)

堆栈用于将华氏值传递给子程序,并将计算出的摄氏值返回给主程序。为此将实施动态堆栈分配。华氏温度和计算出的摄氏温度值都将存储在数据段中定义的分配内存位置中。

到目前为止,我所拥有的是这段代码。当我运行程序时它说

Assemble: operation completed successfully.

它应该要求用户输入华氏温度。但它没有这样做。此外,在用户输入数字后,它应该将其转换为摄氏度并显示结果。

    
    .data
F_temp:     .word   0
C_temp:     .word   0
Number1:    .byte   32
number2:    .byte   5
number3:    .byte   9
enterNumber:    .ascii "\nEnter a temperature in Fahrenheit: \n"
celsiusDegree:  .ascii "\nCelsius temperature is: "
array:      .word 0:25
welcome1:   .ascii " \n This program converts Fahrenheit to Celsius \n\n"

    .text
main:
    la a0, welcome1     #display welcome message
    li x0, 4
    ecall

    la x10,enterNumber             #Ask user to write a number
    li x17,4                  
    ecall                           

    la x6,array                   #store numbers array 
    li x30,25                     #maximum of 25 integers are allowed to be entered 

    # F is in x10               #(F-32) * 5 / 9
    addi x1, x0, 9      #t1 = 9
    addi x2, x2, 5      #t0 = 5
    addi s0, s0, 32     #s0 = 32
    sub x10, x6, s0     #F-32
    mul x10, x6, s0
    div t0, t1, s0
    
done:   
    la x10,celsiusDegree        #display celcius degree
    ecall 


exit:   

    ori a7, zero, 10    # define program exit system call
    ecall           # exit program
4

1 回答 1

2

x0硬连线到0. 进入它永远没有意义lihttps://en.wikichip.org/wiki/risc-v/registers

无论处理程序在哪个寄存器中ecall查找系统调用号,它都不是x0. 检查文档以了解您正在使用的任何内容。(例如RARS 系统调用使用a7,与 MARS 使用 MIPS 寄存器$v0(不是 MIPS $0,零寄存器)的方式相同)


x1混合和t0/s0注册名称通常也是一个坏主意。很容易意外地为同一个寄存器使用 2 个不同的名称,并让您的代码覆盖自己的数据。


在以前版本的问题中,您有:

注意:RISC-V 乘法和除法指令不支持立即数(常量)。因此,所有数值都将在内存中定义并从内存中加载

这很奇怪,“因此”并没有真正遵循。

li reg, constant仍然比 便宜lw,尤其是对于一个小整数。但是如果你的任务说你必须以愚蠢的方式来做,用数据存储器而不是foo = 5汇编.equ时符号常量,那么你必须这样做。你可以在一个地方定义你的常量,但如果你的汇编器不糟糕,你仍然可以将它们用作立即数。

于 2020-11-01T03:28:10.773 回答