为什么当我使用立即值打印整数时它工作正常,但如果我尝试打印包含整数的标签它不会打印任何内容? [英] Why when I print an integer using an immediate value it works fine, but if I try to print a label containing a integer It does not print anything?

查看:89
本文介绍了为什么当我使用立即值打印整数时它工作正常,但如果我尝试打印包含整数的标签它不会打印任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次尝试用 MIPS 语言编写代码.
我尝试使用标签打印,希望它表现得像一个变量,但它没有打印出来.为什么会发生这种情况?

Is my first time trying to code something in MIPS language.
I tried printing using a label, expecting it to behave like a variable, but it doesn't print it. Why is this happening?

印刷直接作品:

    .text
    .globl  main

main:

    # Print label 
    li  $v0,1       # print_int syscall code = 1
    la  $a0, 24     
    syscall

    li  $v0,10      # exit
    syscall

使用标签这样打印是行不通的,没有输出:

printing it like this using a label wont work, there is no output:

    .text
    .globl  main

main:

    # Print label 
    li  $v0,1       # print_int syscall code = 1
    la  $a0, mylabel    
    syscall

    li  $v0,10      # exit
    syscall

    # Start .data segment 
    .data
mylabel:    .word 12

我的逻辑是mylabel"的地址包含数字 12 ,大小为 1 个字,这是 MIPS 中整数的通常大小.但一定有我在那里失踪的东西吗?.谢谢大家.

My logic is that the address of "mylabel" contains the number 12 , and has a size of 1 word which is the usual size for an integer in MIPS. but there must be something I'm missing there?. thanks all.

推荐答案

请记住,mylabel 只是一些引用内存位置的数字.除了用于调试目的外,打印地址可能没有多大意义.

Keep in mind that mylabel is just some number that refers to a memory location. Printing the address probably doesn't make much sense other than for debugging purposes.

la 指令将地址加载到寄存器中,因此当您执行 la $a0, mylabel 时,您是在说加载与 mylabel$a0".

The la instruction loads an address into a register, so when you do la $a0, mylabel, you're saying "load the integer adddress corresponding to mylabel into $a0".

与系统调用 1 关联的 print int 服务将发出 $a0 寄存器中的任何数字.它不会取消引用此内存位置.

The print int service that's associated with syscall 1 is going to emit whatever number is in the $a0 register. It does not dereference this memory location.

如果您想获取 mylabel 的内容,您需要使用 lw 指令(加载字"而不是加载地址"):

If you want to get the contents of mylabel, you'll need to load the memory into a register using the lw instruction ("load word" rather than "load address"):

.text
.globl main

main:
    li $v0 1
    lw $a0 mylabel
    syscall # => 12

    li $v0 10
    syscall

.data

mylabel: .word 12

更进一步,假设您在 mylabel 数组中有两个元素.如果要加载第二个元素,可以使用带有偏移量的 lalw :

Going a step further, let's say you had two elements in the mylabel array. If you want to load the second element, you can use la and lw with an offset:

.text
.globl main

main:
    li $v0 1
    la $t0 mylabel
    lw $a0 4($t0) # 4 byte offset from mylabel
    syscall # => 13

    li $v0 10
    syscall

.data

mylabel: .word 12 13

这篇关于为什么当我使用立即值打印整数时它工作正常,但如果我尝试打印包含整数的标签它不会打印任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆