首页 > 解决方案 > ARM中的字符串长度?

问题描述

试图学习一些基本的ARM,我试图弄清楚如何使用while循环来获取字符串的长度。以下是我在 C 中尝试实现的目标,也是我在 ARM 中尝试实现的目标。我的问题来自 while 循环,我无法遍历字符串,直到我遇到空终止符并使用变量跟踪它。

        char inputString[7];
        
        int lengthOfInput = 0;

        while (inputString[lengthOfInput] != '\0') 
        {
            lengthOfInput++;
        }

    .section .data
    
    #need to include \n if you want to test the output. no idea why
    
    input_prompt    :   .asciz "string: "
    input_spec      :   .asciz  "%[^\n]"
    length_spec     :   .asciz  "length: %d\n" 

    
    #8 bytes for 7 characters plus \0
    input: .space 8
    
    .section .text
    
    .global main
    
    main:
    
        #print prompt
        ldr x0, =input_prompt
        bl printf
        
        #get input
        ldr x0, =input_spec
        ldr x1, =input
        bl scanf
        
        #call get_length
        ldr x9, =input
        ldrsw x0, [x9]
        bl get_length
    
        #print result from get_length
        #to do
    
    
        #exit
        b exit
    
    get_length:
        
        #intializing "i" as 0
        add x19, xzr, xzr
    
        get_length_loop:
            
            #address of userString[i]
            add x12,x19,x1
            #if == 0, go somewhere, maybe get_length should be changed
            cbz x12, get_length
            add x19,x19, 1
            b get_length_loop
        
        #return result
        mov x0, x19
        ret
    
    exit:
        mov x0, 0
        mov x8, 93
        svc 0
        ret

标签: loopsassemblywhile-looparm64

解决方案


推荐阅读