gpt4 book ai didi

Assembly MIPS - 如何将用户的整数存储到内存中?

转载 作者:行者123 更新时间:2023-12-04 21:13:59 26 4
gpt4 key购买 nike

所以,我不知道组装是如何工作的,也不知道我在做什么。我以为我做到了,但我当然错了。所以这是我的问题 - 我什至不知道如何让用户输入一个整数,以便我可以将它存储在内存中。我也不知道我的变量是否对齐,因为我什至不明白“对齐”到底是什么。下面是我的汇编代码,以及说明我希望代码做什么的注释。请帮忙

.data
# variables here

intPrompt: .asciiz "\nPlease enter an integer.\n"
stringPrompt: .asciiz "\nPlease enter a string that is less than 36 (35 or less) characters long.\n"
charPrompt: .asciiz "\nPlease enter a single character.\n"
int: .space 4
string: .space 36
char: .byte 1







.text
.globl main
main:

# print the first prompt
li $v0, 4
la $a0, intPrompt
syscall


# allow user to enter an integer
li $v0, 5
syscall

# store the input in `int`
# don't really know what to do right here, I want to save the user inputed integer into 'int' variable
sw $v0, int
syscall

最佳答案

您应该将“int”变量的类型从 .space 更改为 .word

最后它应该是这样的:

.data
# variables here

intPrompt: .asciiz "\nPlease enter an integer.\n"
stringPrompt: .asciiz "\nPlease enter a string that is less than 36 (35 or less) characters long.\n"
charPrompt: .asciiz "\nPlease enter a single character.\n"
int: .word
string: .space 36
char: .byte 1

main:

li $v0, 4 #you say to program, that you're going to output string which will be in the $a0 register
la $a0, intPrompt #here you load your string from intPromt var. to $a0
syscall #this command just executes everything that you have written before >> it prints string, which is in $a0

li $v0, 5 #this command says: "Hey, read an integer from console and put it in $v0!"
syscall #this command executes all previous commands ( li $v0, 5 )

sw $v0, int #sw -> store word, here you move value from $v0 to "int" variable
syscall #executes (sw $v0, int), here you have your input number in "int" variable

You can find more detailed information here

关于Assembly MIPS - 如何将用户的整数存储到内存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com