【问题标题】:How do you define constants in the MIPS assembly language?如何在 MIPS 汇编语言中定义常量?
【发布时间】:2016-06-17 11:32:30
【问题描述】:

我在网上看了,但我唯一能找到的是这个语法PI = 3.1415,但由于某些莫名其妙的原因,这在我正在使用的 MIPS 模拟器程序 MARS 4.5 中不起作用。如果它是 MIPS 语言规范的一部分,我认为它应该可以工作。当试图组装一个简单的 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

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

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

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

    .eqv SYS_PRINT_STRING 4
    
    li $v0, SYS_PRINT_STRING
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多