阅读字符串并在MIPS中显示前5个字符

发布于 2025-01-24 06:47:44 字数 827 浏览 1 评论 0原文

我正在尝试阅读用户输入字符串并显示前5个字符。我能够将字符串重新打回,但是我从界限中获得内存地址最有可能在line 移动$ a0,$ t8,因为我的分配给> lb $ t8是错误的。我不能做这项工作。

#data segment
.data
    buffer: .space 20       #allocate space for 20 bytes=characters

#text segment
.text
    .globl __start 

#program start
__start:

    #get user input
    li $v0,8
    #load byte space
    la $a0,buffer
    #tell the system the max length
    li $a1,20
    move $t1,$a0
    syscall

    #display the input
    li $v0,4
    syscall

    #print 5 first characters
    li $t6,5

loop:   lb $t8,($t1)

    li $v0,4
    la $a0,buffer
    move $a0,$t8
    syscall 

    add $t1,1
    
    #1 less letter to print
    sub $t6,1

    #have we printed all 5
    bne $t6,0,loop

T1是否具有第一个字符串的字节?

赞赏此问题以外的所有帮助和/或任何一般技巧。

I am trying to read a user input string and display the first 5 characters. I am able to get and print the string back, but I get Memory Address out of bounds most possibly in line move $a0,$t8 because my assignment to lb $t8 is wrong. I can not make this work.

#data segment
.data
    buffer: .space 20       #allocate space for 20 bytes=characters

#text segment
.text
    .globl __start 

#program start
__start:

    #get user input
    li $v0,8
    #load byte space
    la $a0,buffer
    #tell the system the max length
    li $a1,20
    move $t1,$a0
    syscall

    #display the input
    li $v0,4
    syscall

    #print 5 first characters
    li $t6,5

loop:   lb $t8,($t1)

    li $v0,4
    la $a0,buffer
    move $a0,$t8
    syscall 

    add $t1,1
    
    #1 less letter to print
    sub $t6,1

    #have we printed all 5
    bne $t6,0,loop

Does t1 not have the byte of the first string?

All help and/or any general tips outside of this problem is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画离情绘悲伤 2025-01-31 06:47:44

移动指令不可能导致内存出异常。 只有加载和存储说明才能引起这一点。

您正在使用Syscall#4打印单个字节,因此SYSCALL试图将$ A0中提供的字节作为指针,而作为不匹配,这将是错误的。

lb $t8,($t1)

li $v0,4
la $a0,buffer # putting buffer address in $a0
move $a0,$t8  # overwriting the buffer address with byte value in $t8
syscall 

There is no possibility that a move instruction causes a memory out of bounds exception.  Only load and store instructions can cause that.

You are using syscall #4 to print a single byte, so the syscall attempts to dereference the byte provided in $a0 as a pointer, and as a mismatch, that's going to fault.

lb $t8,($t1)

li $v0,4
la $a0,buffer # putting buffer address in $a0
move $a0,$t8  # overwriting the buffer address with byte value in $t8
syscall 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文