ArmSim程序集比较输入字符串中的位

时间:2014-10-28 00:28:27

标签: assembly arm

我正在尝试编写一个从输入文件中读取整个字符串的程序。然后,它用符号" *"替换字母。和数字用符号"#"。其他任何东西都是一样的。我让我的程序读取字符串并检查第一位以查看它是数字还是字母。如何移动到下一位并继续比较直到字符串结束? 这个我的代码到目前为止:

   .equ SWI_Open,0x66     @ open a file
.equ SWI_Close,0x68    @ close a file
.equ SWI_PrStr,0x69    @ Write a null-ending string 
.equ SWI_RdStr,0x6a    @ read a string from file
.equ SWI_PrInt,0x6b    @ Write an Integer
.equ Stdout, 1         @ Set output target to be Stdout
.equ SWI_Exit,0x11     @ Stop execution
.global _start
_start:
@ ========= Open file for reading =============================
ldr r0,=myFile        
mov r1,#0       
swi SWI_Open          @ open file
bcs InFileError       @ if cannot open file branch to InFileError 
ldr r1,=InputFileHandle
str r0,[r1]
@ ========== Read String ======================================= 
ldr r0,=InputFileHandle
ldr r0,[r0]
ldr r1,=array
mov r2,#1024
swi SWI_RdStr
bcs emptyFile        @ branch if file is empty
mov r3,#0
mov r5,r0            @ number of characters
mov r6,r1            @ address of string
mov r7,#0x30
mov r8,#0x39
mov r9,#0x41
mov r10,#0x5A
mov r11,#0x61
mov r12,#0x7A
@ ===============================
Loop:
ldrb r4,[r6]
cmp r4,r7
BLT sim
cmp r4,r8
BLE Num
cmp r4,r9
BLT sim
cmp r4,r10
BLE Letter
InFileError:
mov R0, #Stdout
ldr R1, =FileOpenInpErrMsg
swi SWI_PrStr 
bal Exit

emptyFile:
mov R0, #Stdout
ldr R1, =FileEmpty
swi SWI_PrStr 
bal Exit 

Letter:
mov r4,#'*
strb r4,
add r4,r4,#1
bal Loop

Num:


sim:
add r5,r5,#1

InputFileHandle: .word 0
array: .skip 1024
FileEmpty: .asciz "File is Empty"
myFile: .asciz "input.txt"
FileOpenInpErrMsg: .asciz "Error opening file \n"
EndOfFileMsg: .asciz "End of file reached\n"
NL: .asciz "\n " @ new line
.end

2 个答案:

答案 0 :(得分:0)

  • 由于r6包含字符串的地址,因此如果是字母或数字,则需要更改的内存位置
  • 这也意味着它需要被提升1才能到达字符串
  • 中的下一个字符
  • 小心使用bal;如果它代表分支和链接,那么你应该只使用它来跳转到子程序(链接是你从它那里取回的方式)

答案 1 :(得分:0)

使用: ldrb r4,[r6],#1 代替 ldrb r4,[r6]