Hello all,

Let me first state that this is my first program written in ARM, so please excuse that it might seem a bit chaotic. Also, I want to state that this is a homework assignment for an assembly class. I am supposed to read in an input file, only print out ascii chars space, A-Z, and a-z (toUpper as A-Z) as well as Carriage Return Line Feeds and a CRLF as the 1A (EOF char). Going through the debugger it seems to be working as intended, but it isn't writing to the outfile :( If anyone can point out the error to me I would be very grateful. I know there is a lot of optimization that can be done to make the code smaller, but none of that matters really if I can't get the file to write. Thank you very much for your time.

;----------------------------------
; Software Interrupt values
;----------------------------------
         .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 and terminate with null char
         .equ SWI_Exit,  0x11     ;Stop execution
;----------------------------------

         .global   _start
         .text

_start:
         ldr  r0, =InFileName     ;r0 points to the file name
         ldr  r1, =0              ;r1 = 0 specifies the file is input
         swi  SWI_Open            ;open the file ... r0 will be the file handle
         ldr  r1, =InFileHandle   ;r1 points to handle location
         str  r0, [r1]            ;store the file handle

         ldr  r0, =OutFileHandle  ;r0 points to the output file handle
         ldr  r1, =1
         swi  SWI_Open
         ldr  r1, =OutFileHandle
         str  r0, [r1]

_read:        
         ldr  r0, =InFileHandle
         ldr  r0, [r0]
         ldr  r1, =InString
         ldr  r2, =80
         swi  SWI_RdStr

         ldr  r0, =InString       ;r0 points to the input  string
         ldr  r1, =OutString      ;r1 points to the output string
_loop:                            ;
         ldrb r2, [r0], #1        ;get the next input byte
                                  ; then increment the input pointer
         b    _testZ
_cap:
         sub  r2,r2,#32           ; subtract 32 to make it uppercase ascii
_testZ:
         cmp  r2,#90               ;see if it is above ascii "Z"
         bgt  _cap
_testA:
         cmp  r2,#65               ; see if it is above ascii "A"
         bgt  _store               ; if it is > "A" and <="Z" it must be A-Z
_testch:
         cmp  r2,#32              ;it is a space 
         beq  _store
_testeof:
         cmp  r2,#26               ;eof character - go to write
         beq  _write
_testeol:
         cmp  r2,#0                ; eol character
         bne  _loop
_store: 
         strb r2,[r1],#1            ; store the byte and inc pointer by 1
         cmp  r2,#0                 ; if it's the end of the line fall through to write
         bne  _loop
_write:   
         ldr  r0, =OutFileHandle
         ldr  r0, [r0]
         ldr  r1, =OutString 
         swi  SWI_PrStr           ;write the null terminated string  
         ldr  r1, =CRLF
         swi  SWI_PrStr
         cmp  r2,#0x1A
         bne  _read 
_exit:                            
         ldr  r0, =InFileHandle   ;r0 points to the input  file handle
         ldr  r0, [r0]            ;r0 has the input file handle
         swi  SWI_Close           ;close the file
                                  ;
         ldr  r0, =OutFileHandle  ;r0 points to the output file handle
         ldr  r0, [r0]            ;r0 has the output file handle
         swi  SWI_Close           ;close the file
                                  ;
         swi  SWI_Exit            ;terminate the program

         .data
;----------------------------------
InFileHandle:  .skip 4            ;4 byte field to hold the input  file handle
OutFileHandle: .skip 4            ;4 byte field to hold the output file handle
                                  ;
InFileName:    .asciz "KEY.IN"   ;Input  file name, null terminated

InString:      .skip 128          ;reserve a 128 byte string for input
OutString:     .skip 128          ;reserve a 128 byte string for output
                                  ;
CRLF:          .byte 13, 10, 0    ;CR LF
                                  ;
OutFileName:   .asciz "FILE.OUT"  ;Output file name, null terminated
;----------------------------------
         .end

Recommended Answers

All 5 Replies

Are you sure your output string is null-terminated?

You seem to be using armsim?

Yes, I'm using armsim. I'm not sure how to tell if it is being null terminated for sure or not. Could that be the problem here? Is there any way that I could check to be for sure?

Line 21 should be OutFileName and not Handle. That being said if anyone sees any ways to make the code better I would gladly take your advice. Thanks turbo for the response.

Line 21 should be OutFileName and not Handle.

Escaped my eyes too. Good that you noticed it.

The string is null-terminated if you add null at the end of it.

    _testeof:
             cmp  r2,#26               ;eof character - go to write
             beq  _write
    _testeol:
             cmp  r2,#0                ; eol character
             bne  _loop
    _store: 
             strb r2,[r1],#1            ; store the byte and inc pointer by 1
             cmp  r2,#0                 ; if it's the end of the line fall through to write
             bne  _loop
    _write:  
             ; if EOF, you come here without storing NUL
             ; at the end of output string. If there was a NUL, you would have
             ; come here before encountering EOF.
             ldr  r0, =OutFileHandle

Yes.... You are right that line 21 should be OutFileName and not Handle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.