在armV7中计算字符串中的字符 [英] Counting Characters in a String in armV7

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

问题描述

我的程序应该要求用户输入一行,然后打印出字符串中的字符数.截至目前,它告诉我输入hello时出现104个字符,然后出现分段错误.

My program is supposed to ask for a single line of user input and then print out the number of characters in the string. As of now it is telling me there are 104 characters when I input hello followed by a segmentation fault.

这是我的代码:

userInput:
    .asciz "\nEnter a string: " 

TemptRet:
    .word 10


inputBuffer:    
    .skip   11

countMessage:
    .STRING "There are %d characters in: \"%s\".\n"



.text
.global main


main:
    LDR R0, =courseStr
    BL puts

countString:
    LDR R0, =userInput
    BL printf

    LDR R0, =TemptRet
    BL scanf

getLine:
    MOV R2, R0
    BL getchar


    LDR R2, =inputBuffer
    MOV R1, R0
    LDR R0, =countMessage
    BL printf

推荐答案

一些建议.

您使用 scanf 读取字符串时,将停止在输入的第一个空格处.取而代之的是,您可以使用 fgets ,看起来有些复杂,但是了解ARM过程调用约定非常容易.

Your use of scanf to read in a string is going to stop at the first space in your input. Instead you can use fgets which looks a bit more complicated but with understanding the ARM procedure calling convention is quite easy.

第二,将您的 .data 部分移至末尾,并以 push {ip,lr} 开头,以便您的例程可以以 pop {ip,pc} .

Second, move your .data section to the end and start with push {ip, lr} so your routine can end with pop {ip, pc}.

考虑在装配体中使用注释来了解每一行的内容.用高级语言编写而没有注释已经够糟糕的了,在汇编中,忘记所做的事情甚至更加容易.

Consider using comments in your assembly to understand what each line is doing. Writing in a high-level language without comments is bad enough, in assembly it is even easier to forget what you were doing.

下面的代码有几个关键部分:

There are several key sections to the code below:

  • 提示用户输入字符串
  • 设置r0,r1和r2以使用 fgets (要特别注意将 FILE * stdin 的值加载到r2中的使用)
  • 计算字符串的长度并切掉换行符
  • 打印结果
  • Prompting the user to enter a string
  • Setting up r0, r1, and r2 to utilize fgets (pay particular attention to the use of loading the value of FILE* stdin into r2)
  • Calculating the length of the string and chopping off the newline
  • Printing the result

input.s :

.global main

main:
    push {ip, lr}

    ldr r0,=prompt // r0 <- prompt*
    bl printf

    ldr r0,=buffer // r0 <- buffer*
    mov r1,#buflen // r1 <- buflen
    ldr r2,=stdin
    ldr r2,[r2]    // r2 <- STDIN
    bl  fgets      // fgets(buffer, buflen, STDIN)
    bl strlen      // r0 <- strlen(buffer)
    mov r1,r0      // r1 <- strlen(buffer)
    sub r1,#1      // r1 <- r1 - 1

    ldr r0,=output // r0 <- output*
    ldr r2,=buffer // r2 <- buffer*
    mov r4,#0
    str r4,[r2,r1] // r4(NULL) -> buffer + strlen
    bl printf

    pop {ip, pc}

.data
prompt:.asciz "Enter a string: "
output:.asciz "There are %d characters in: \"%s\".\n"
buffer:.space 128
buflen=.-buffer

pi@raspberrypi:~ $ gcc input.s -o input

pi@raspberrypi:~ $ ./input
Enter a string: this is a string of moderate length
There are 35 characters in: "this is a string of moderate length".

祝你好运.

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

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