简体   繁体   English

RISC-V 汇编语言程序将华氏温度转换为摄氏温度

[英]RISC-V Assembly Language Program to Convert Fahrenheit to Celsius

An assembly language program to convert a temperature value given in Fahrenheit to Celsius.将华氏温度值转换为摄氏温度值的汇编语言程序。 The formula to be implemented is 𝐶 = (𝐹 − 32) × 5⁄9.要实现的公式是𝐶 = (𝐹 − 32) × 5⁄9。 Data Segments Required:所需的数据段:

  1. F_temp (word) F_temp(字)
  2. C_temp (word) C_temp(字)
  3. The value 32 (byte)值 32(字节)
  4. The value 5 (byte)值 5(字节)
  5. The value 9 (byte)值 9(字节)
  6. Prompt for input (string)输入提示(字符串)
  7. Message for output (string)输出消息(字符串)

The stack is to be used for passing the Fahrenheit value to the subroutine and for returning the calculated Celsius value back to the main program.堆栈用于将华氏温度值传递给子程序,并将计算出的摄氏度值返回给主程序。 Dynamic stack allocation is to be implemented for this.为此将实现动态堆栈分配。 Both the Fahrenheit and the calculated Celsius values are to be stored in the allocated memory locations defined in the data segment.华氏度和计算出的摄氏度值都将存储在数据段中定义的分配内存位置。

What I have so far is this code.到目前为止,我拥有的是这段代码。 When I run the program it says当我运行程序时,它说

Assemble: operation completed successfully.

It is supposed to ask the user to enter a Fahrenheit temperature.它应该要求用户输入华氏温度。 But it not doing that.但它没有这样做。 Also, after the user enters a number, it should convert it to Celsius and display the result.此外,用户输入数字后,应将其转换为摄氏度并显示结果。

    
    .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

x0 is hard-wired to 0 . x0硬连线到0 It never makes sense to li into it.它从来没有有意义的li进去。 https://en.wikichip.org/wiki/risc-v/registers . https://en.wikichip.org/wiki/risc-v/registers

Whatever register the ecall handler looks in for a system-call number, it's not x0 .无论ecall处理程序查找系统调用号的寄存器是什么,它都不是x0 Check the documentation for whatever you're using.检查文档以了解您正在使用的任何内容。 (eg RARS system-calls use a7 , the same way that MARS used the MIPS register $v0 (not MIPS $0 , the zero register)) (例如RARS 系统调用使用a7 ,与 MARS 使用 MIPS 寄存器$v0方式相同(不是 MIPS $0 ,零寄存器))


Also generally a bad idea to mix x1 and t0 / s0 register names.混合x1t0 / s0寄存器名称通常也是一个坏主意。 Easy to accidentally use 2 different names for the same register and have your code overwrite its own data.容易意外地为同一个寄存器使用 2 个不同的名称,并让您的代码覆盖其自己的数据。


In a previous version of the question you had:在之前版本的问题中,您有:

Note: RISC-V multiply and divide instructions do not support immediates (constants).注意:RISC-V 乘除指令不支持立即数(常量)。 Therefore, all numeric values are to be defined in and loaded from memory因此,所有数值都将在内存中定义和加载

That's weird, the "therefore" doesn't really follow.这很奇怪,“因此”并没有真正遵循。

li reg, constant is still cheaper than lw , especially for a small integer. li reg, constant还是比lw便宜,尤其是对于小整数。 But if your assignment said you had to do it the stupid way, with data memory instead of foo = 5 or .equ assemble-time symbolic constants, then you have to.但是如果你的作业说你必须用愚蠢的方式来做,用数据存储器代替foo = 5.equ汇编时符号常量,那么你必须这样做。 You can define your constants in one place, but then still use them as immediates, if your assembler doesn't suck.你可以在一个地方定义你的常量,但如果你的汇编程序不烂,仍然可以将它们用作立即数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM