Reading and Printing a string in arm assembly

≡放荡痞女 提交于 2019-12-22 18:29:23

问题


I am using ARMSim and have just started learning Assembly so please excuse me if i look clueless but I am trying to Read a string from an input file and then printing it to output screen. So far i have :

.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

So far when i run it i can print my initial message fine. The issue is it is not reading to the end of the file and I am not getting my End of file message. Any pointers would be great.

来源:https://stackoverflow.com/questions/26053510/reading-and-printing-a-string-in-arm-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!