如何在 MIPS 汇编语言中定义常量?

How do you define constants in the MIPS assembly language?

我在网上查了一下,但我唯一能找到的是这种语法 PI = 3.1415,但由于某些无法解释的原因,这在我使用的 MIPS 模拟器程序中不起作用 MARS 4.5.如果它是 MIPS 语言规范的一部分,我认为它应该可以工作。当尝试 assemble 一个简单的 hello world 程序时,编译器说我的代码中有一个无效的语言元素。这是代码本身:

################################################################################
#                                                                              #
# This is a hello world program in the MIPS assembly language. It prints out   #
# "Hello, World!" on the screen and exits.                                     #
#                                                                              #
################################################################################

# System call code constants
SYS_PRINT_STRING = 4
SYS_EXIT         = 10

.data
    msg: .asciiz "Hello, World!\n"

.text
.globl __start
__start:
    la $a0, msg              # Load the address of the string "msg" into the
                             # $a0 register
    li $v0, SYS_PRINT_STRING # Store the system call for printing a string on
                             # the screen in the $v0 register
    syscall

    li $v0, SYS_EXIT         # Store the system call to exit the program in the
                             # $v0 register
    syscall

你想要的是替换宏。 MIPS 是一个指令集,除了指令本身,它没有定义太多。指令集通常没有像高级语言那样的命名常量的概念。替换宏是由汇编器实现的,所以你需要检查你的汇编器文档。

对于 MARS,指令是 .eqv。示例:

.eqv SYS_PRINT_STRING 4

li $v0, SYS_PRINT_STRING