【发布时间】:2016-05-30 18:34:01
【问题描述】:
所以我刚刚在树莓派(用于 ARM 处理器)上运行我的代码,当我运行可执行文件时它会引发分段错误。这是我的编译方式:
gcc -o cw2 cw2.s
我不知道为什么!它需要一个字符流的输入文件(只是文本)。
这是我的 ARM 汇编代码:
.global main
.global printf
.global fopen
.global fclose
.global getc
main:
; Open File
PUSH {R1} ; Push address of R1 onto stack
LDR R0, [R1, #0x04] ; Get argv[1] from stack to R0
LDR R1, =r ; Load address of file open format (read)
BL fopen ; Open file
LDR R1, =fin ; Load address of file in to R1
STR R0, [R1] ; Store contents of R1 into R0
; Setup array
LDR R4, =ch_array ; Array address
MOV R3, #0 ; Array index
BL loopFile
BL printArray
@ Loop through the file
loopFile:
LDR R1, =fin ; Load R1 with address of file in
LDR R0, [R1] ; Load R0 with pointer? of R1
BL getc ; Get next character from file into R0?
CMP R0, #-1 ; Check for end of file
BEQ endl ; Close file
; get index from character (getc)
/*
Assuming the base address is in R4 (my array), the ascii is in R0
from the file input into R0: increment the word in memory at
the address 'R4 + (R0 * 4)'. *4 because each word is 4 bytes.
*/
LSL R0, R0, #2
LDR R1, [R4, R0]
ADD R1, R1, #1
STR R1, [R4, R0]
B loopFile ; Run loop again
@ Close the file
endl:
LDR R1, =fin ; Load R1 with address of file in
LDR R0, [R1] ; Load R0 with pointer? of R1
BL fclose ; Close the file
printArray:
CMP R3, #ARRAY_MAX ; while (i < ARRAY_MAX)
BEQ _exit ; Exit if max
; Check if value of the array at index i where
; (i = character number) is 0, if so then skip it
; else print it
B printArray ; Loop next iteration of array
_exit:
MOV R7, #1
MOV R0, #0
SWI 0
.data
.equ ARRAY_MAX, 255
ch_array:
.rept ARRAY_MAX ;For all elements in array, repeat:
.word 0x00 ;Initialize to 0
.endr ;End repetition
fin: .word 0x00
r: .asciz "r"
space: .word ' '
nl: .word '\n'
我也不知道如何测试打印出数据以查看它是否真的在读取任何内容。
【问题讨论】:
-
使用调试器。 PS:你应该使用
fgetc而不是getc,因为后者可能是一个宏。但是,如果您的代码已编译,那么它不是并且应该可以正常运行。 -
@Jester 我将如何遍历我的数组以查看是否存储了值?