本文介绍了ARM汇编:获取从标准输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯目前在CS课程,我们刚刚开始与ARM大会上树莓派工作。它被证明是体面的困难和想知道如果任何人都可以提供帮助。我目前的任务是采取从标准输入(使用scanf函数)的字符串并计算它的字符数,然后返回这个数字(所以基本上实现自己的strlen)。我的基本思想来与此code:

Im currently in a CS course and we've just started working with ARM Assembly on the Raspberry Pi. It's proving to be decently difficult and was wondering if anyone could help. My current assignment is to take a string from stdin (Using scanf) and count the number of characters in it, then return that number (So basically implement my own strlen). I have the basic idea down with this code:

.section        .rodata
promptWord:
    .ascii  "Enter a word: \000"

readWord:
    .ascii  "%s\000"

printLength:
    .ascii  "Word length is %d characters.\n\000"

.section .data
    .align 2

    .comm   word,4,4
    .text

addrword:  .word word
addrPromptWord: .word promptWord
addrReadWord: .word readWord
addrPrintLength: .word printLength



    .global  main
                                    /* s: r0 */

main:
    stmfd   sp!, {fp, lr}       /* Save pc, lr, r4*/

    ldr r0, addrPromptWord
    bl  printf

    ldr r0, addrReadWord
    ldr r1, addrword
    bl  scanf

    ldr r0, addrword
    ldr r0, [r0]

    mov r1, #0
skip:
    ldrb    r2,[r0]                 /* r2 <- *a */
    mov     r3,#0
    cmp     r2,r3
    beq     endskip                 /* if (*a == 0) jump endskip */
    mov     r3,#1
    add     r0,r0,r3                /* a++ */
    add     r1, r1, r3              /* len++ */
    bal     skip                    /* go to skip */

endskip:
    mov r0, r1                      /* Return len */

    ldmfd   sp!, {fp, pc}  

我假设的问题是,因为code的.data段部分(我假设)的心不是对准一个字符串的正确方法。任何帮助深表AP preciated。谢谢!

I'm assuming that the issue is with the .data section part of the code since (I'm assuming) that isnt the proper way to align a string. Any help is much appreciated. Thanks!

推荐答案

为什么不写一个C程序做同样的事情,并运行

Why not write a C program do the same thing, and run

gcc -S file.c

你会看到编译器如何处理它file.s(GCC所生成的汇编code)。即使你不明白file.s一些行,它会导致你的正确的地方臂总成手册。

That you will see how the compiler deal with it in file.s(assembly code generated by gcc).Even if you do not understand some lines in file.s, it would lead you to the right place of arm assembly manual.

这是没有直接回答你的问题。但可悲的我不能评论你的帖子,否则我会做到这一点。

This is not directly answer of your question. But sad I can not comment your post, otherwise I would do that.

这篇关于ARM汇编:获取从标准输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:23