在臂组件中读取和打印字符串

时间:2014-09-26 06:41:41

标签: string assembly input arm output

我正在使用ARMSim,刚刚开始学习汇编,所以请原谅我,如果我看起来一无所知,但我试图从输入文件中读取一个字符串,然后将其打印到输出屏幕。到目前为止我有:

.equ SWI_Open, 0x66 @open a file
.equ SWI_Close,0x68 @close a file
.equ SWI_PrChr,0x00 @ Write an ASCII char to Stdout
.equ SWI_PrStr, 0x69 @ Write a null-ending string 
.equ SWI_PrInt,0x6b @ Write an Integer
.equ SWI_RdInt,0x6c @ Read an Integer from a file
.equ Stdout, 1 @ Set output target to be Stdout
.equ SWI_Exit, 0x11 @ Stop execution
.equ SWI_RdStr, 0x6a @ Read string from file
.equ SWI_PrStg, 0x02 @print to Stdout
.global _start
.text
_start:

mov R0,#Stdout @print an initial message 
ldr R1, =Message1 @ load address of Message1 label
swi SWI_PrStr @ display message to Stdout


ldr R0,=InputName @ set Name for input file
mov R1,#0 @ mode is input
swi SWI_Open @ open file for input
bcs InFileError @ Check Carry-Bit (C): if= 1 then ERROR

RLoop:
ldr r0,=InputHandle @ load input file handle
ldr r0,[r0]
ldr R1,=CharArray
mov R2,#80
swi SWI_RdStr
bcs EofReached @ Check Carry-Bit (C): if= 1 then EOF reached

@ print the string to Stdout
mov R1,R0  @R1 = string to print
mov R0,#Stdout @ target is Stdout
swi SWI_PrStr
mov R0,#Stdout @ print new line
ldr R1, =NL
bal RLoop @ keep reading till end of file




EofReached:
mov R0, #Stdout @ print last message
ldr R1, =EndOfFileMsg
swi SWI_PrStr


  ldr R0, =InputHandle @ get address of file handle
  ldr R0, [R0] @ get value at address
  swi SWI_Close

Exit:
swi SWI_Exit @ stop executing 
InFileError:
  mov R0, #Stdout
  ldr R1, =FileOpenInpErrMsg
  swi SWI_PrStr 
  bal Exit @ give up, go to end


  .data
  .align
InputHandle: .skip 4
CharArray: .skip 80
InputName: .asciz "whatever.txt"
FileOpenInpErrMsg: .asciz "Failed to open input file \n"
EndOfFileMsg: .asciz "End of file reached\n"
ColonSpace: .asciz": "
NL: .asciz "\n_" @ new line 
Message1: .asciz "Reading String from udp.dat and printing out ASCII values. \n"
.end

到目前为止,当我运行它时,我可以打印我的初始信息。问题是它没有读到文件的末尾,我没有得到我的文件结束消息。任何指针都会很棒。

0 个答案:

没有答案