如何保存字符串并添加到它?

时间:2014-01-30 14:43:05

标签: assembly mips

所以,我们从一所学校得到了一个程序,用于计算从字符串中读取一个字符,将其变成一个数字,然后继续对其进行某些计算,最终获得一个0-9之间的数字。我做到了。

下一部分让我头疼。我需要将这些信息存储到某个内存地址中,这样它就有原始字符串,我得到的数字和最后的NULL。我无法理解。

这是我的代码,已翻译评论并删除了个人信息:

#Memory storages and their use
#s1 The location for the original string
#s2 How long the string is
#s3 Character/Number we are handling
#s4 Location of the end result ($s1 + space(0x20) + outcome)
#s5 Outcome from the multiplications
#s6 Help for loops  
#s7 How many times to repeat multiplications

.globl  alku

.data   0x10010000

VIITE:      .asciiz "213869"    #The string I am operating
VIITENUMERO:    .space  22      #Where I am apparently supposed to place data

ILMOITUS:   .asciiz "Annettu viite sisälsi ei-numeroita"

.text   0x00400000

alku:   
    la  $s1, VIITE              #Load the string we are dealing with
    la  $s4, VIITENUMERO        
    la  $s6, 1          #Loop set to 1

laskePituus:
    lb  $t1, 0($s1)     
    beq $t1, $zero, jatka   #Are we at the end?
    addi    $s2, $s2, 1             #No we are not, move forward.
    addi    $s1, $s1, 1
    j   laskePituus


jatka:  
    addi    $s1, $s1, -1            #We move out of NUL


laske:  addi    $s2, $s2, -1            #Reduce the length/loop counter
    lb  $a0, 0($s1)         #Load byte, AKA character we are dealing
    jal onkoNumero                      #Go check if it really is a number
    lb  $a0, 0($s1)         #Load it... again :/ Just to make sure it does not disappear
    jal muutaNumero         #We go and turn it into a number
    beq $s6, 1, kerro7          #Check what we use to multiply with
    beq $s6, 2, kerro3
    beq $s6, 3, kerro1
laskeJatka: 
    beq $s2, $zero, poistaKymmenen  #We are back, are we at the end?
    addi    $s1, $s1, -1            #No, we move to next character
    j   laske

poistaKymmenen:                             #We reduce the outcome until we have <10
    blt     $s5, 10, muutaMerkiksi
    addi    $s5, $s5, -10
    j   poistaKymmenen

muutaMerkiksi:
    addi    $s5, $s5, 0x30          #We turn the outcome into an ASCII character
    j   LOPETA


VIRHE:
    la      $a0, ILMOITUS           #Error is printed incase there is a non-number
    li  $v0, 4
    syscall                 # Print it
LOPETA: 
    li  $v0, 10             
    syscall                 # End Program   

#FUNCTIONS

muutaNumero:
    lb  $t1, 0($s1)         #Load byte
    addi    $t1, $t1, -0x30         #Number characters are at the range 30-39, so reduce 30 to get an int
    la  $s3, ($t1)          #Save it
    jr  $ra             #Return

kerro7:
    la  $s7, 7          
    addi    $s6, $s6, 1
    jal kertoLasku

kerro3:
    la  $s7, 3
    addi    $s6, $s6, 1
    j   kertoLasku

kerro1:
    la  $s7, 1
    la  $s6, 1
    j   kertoLasku

kertoLasku:
    add $s5, $s5, $s3           #Add numbe
    addi    $s7, $s7, -1            #Reduce counter
    beq $s7, $zero, laskeJatka      #Are we at the end?
    j   kertoLasku          #No, loop

onkoNumero:                 #Checks that the characters is actually a number
    lb  $t1, 0($s1)
    blt $t1, 0x30, VIRHE        #Less than 30?
    bgt     $t1, 0x39, VIRHE        #More than 39?
    jr  $ra             #Is a number, return

如果可能的话,当你发布答案时,最好解释每一个动作,以便下次我需要这些知识,我实际上知道我在做什么。我宁愿知道我需要做什么和为什么,而不仅仅是复制和回答。

0 个答案:

没有答案