在 MIPS 中空终止字符串? [英] Null-terminating a string in MIPS?

查看:56
本文介绍了在 MIPS 中空终止字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MIPS 中编写 strncpy,但是我在用空终止字符串时遇到了麻烦.如果我自己不空终止它,则字符串会继续.我试过 sb $__ 0($0) 但这似乎不起作用......

I'm writing strncpy in MIPS, but I'm having trouble null-terminating the string. If i do not null-terminate it myself, the string goes on and on. I have tried sb $__ 0($0) but that does not seem to work...

$a0 = 指向目标数组的指针
$a1 = 源字符串
$a2 = 要复制的字符数

strncpy: 
    add $t1 $zero $zero  #counter 
    beq $a2 $0 done # if num chars to copy is 0, return.
    j cpyLoop

cpyLoop:    

    beq $t1 $a2 done # if counter == num to copy, end
    lb $t2 0($a1) # load the character
    beq $t2 $0 done #if we reach the null char, end
    sb $a0 0($a1)
    addi $a0 $a0 1 #increment the pointer in dest array
    addi $a1 $a1 1 #increment the pointer in source array
    addi $t1 $t1 1 #increment the counter
    j cpyLoop



done:   
    lb $a0 0(0)
    move $v0 $a0 
    jr $ra

推荐答案

为了使这篇文章完整,我将从上面提到的重复的 poast 中提取一部分并完成它:

to make this post complete i will take part of coat from the dublicate poast mentioned above and complete it:

addi $a3 $a0 0


strncpy:   
        beqz $a2, out
        lb $t0, 0($a1)      #load byte
        beqz $t0 out 
        subiu $a2, $a2, 1
        sb $t0, 0($a0)
        addiu $a0, $a0, 1
        addiu $a1, $a1, 1
        j strncpy


 out:
        move $a0 $a3
        move $v0 $a0
        jr $ra

这篇关于在 MIPS 中空终止字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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