我如何在此臂代码中测试输入的有效性?

问题描述

下面是一些分部代码,我只需要帮助进行验证。该程序应该测试寄存器6和7是否大于或等于0且小于或等于268,435,455。如果它们都为0,则程序结束。但是,如果它们在参数内,则将它们添加(以16为基数),并且程序会计算代码中发生了多少次移位。例如,给定两个输入“ 13398 31898”,它将打印2。“ 268435455 1”应打印“ 7”。 该代码在用于这些验证和计数的区域中的2条注释行之间注释。寄存器1在末尾接收移位数。

目标: 测试0 如果为true,则从16开始加法。如果为false,则将它们发送到bad_int。如果它们都等于0,则程序结束。

    .equ SWI_Exit,0x11             @ exit gracefully
    .equ SWI_Close,0x68            @ close file
    .equ SWI_Open,0x66             @ open file
    .equ SWI_ReadINT,0x6c          @ read integer from fd in R0 -> R0
    .equ SWI_WriteINT,0x6b         @ write integer in R1 to fd in R0
    .equ SWI_WriteS,0x69           @ write string to stdout

    .text                       
    .global _start             
  _start:                                 @ The simulator will look for "_start".
    ldr     r0,=filename           @ R0 is the address of the file
    mov     r1,#0                  @ R1 = 0 so we open it "read only"
    swi     SWI_Open                @ Do the software interrupt to open the file
    bcs     file_not_found          @ "Branch if carry set" if it did not work.
    ldr     r1,=file_handle        @ Otherwise save the file handle for reading.
    str     r0,[r1]                @ It is stored at "file_handle"

    @ Here's the top of the "loop" in terms of reading the numbers
    @ and seeing if they are not both zero. We'll put the first number
    @ in R6 and the second in R7 (arbitrary choices).
    
  loop:   ldr     r1,=file_handle        @ Load the file handle 
    ldr     r0,[r1]                @ It goes into R0
    swi     SWI_ReadINT             @ Ask the operating system to read one number.
    mov     r6,r0                  @ Let's put the first number in R6 (just because)
    
    ldr     r1,=file_handle        @ Load the file handle again
    ldr     r0,[r1]
    swi     SWI_ReadINT
    mov     r7,r0                  @ Let's put the second number in R7

    @ Here you will need to test to see if both numbers are zero,@ within the range in the assignment,and so on.
    @ If they are,then continue,else branch down to where
    @ the error prints out.

  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    
  @ Here's where you come in!
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    
    cmp     r6,#0                  @ First value less than 0?
    blt     bad_int                 @ Bad int if less than 0
    cmp     r6,#268,455        @ First value greater than 268,455?
    bgt     bad_int                 @ Bad int if greater than 268,455
    cmp     r7,#0                  @ Second value greater than 0
    blt     bad_int                 @ Bad int if less than 0
    cmp     r6,455

    cmp     r6,r7                  @ Are both equal to zero?
    beq     0,end



   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ Put your answer into R1 so that we can print it
    mov     r0,#1                  @ stdout
    swi     SWI_WriteINT
    ldr     r1,=newline
    swi     SWI_WriteS
    b       loop                    @ Next set of numbers please.

bad_int:
    mov     r0,#1                  @ Stdout
    ldr     r1,=bad_string         @ Write the bad data string
    swi     SWI_WriteS
    b       loop
    
end_of_file:
    ldr     r0,=file_handle
    ldr     r0,[r0]
    swi     SWI_Close
    swi     SWI_Exit

file_not_found:                     
    mov     r0,#1                  @ In the case where we Could
    ldr     r1,=in_file_error      @ not open the file,of course
    swi     SWI_WriteS              @ there is no need to close it.
    swi     SWI_Exit                @ So bail out.

 @ ============================================================
 @ Data
 @ ============================================================
    
    .data

    @ Any "word" must be aligned on four bytes
    .align  4
 file_handle:    .word 0x00

    @ Strings can be aligned anywhere.
 filename:       .asciz  "CARRY.TXT"
 in_file_error:  .asciz  "No such file???\n"
 bad_string:     .asciz  "The file contains invalid data!\n"
 newline:        .asciz  "\n"

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)