简体   繁体   中英

Cannot open disk file using assembler syscalls on aarch64 device

I am a Hobby programmer here now trying to get into assembler for an android phone. I cannot open a disk file. I would dearly like to get past this hurdle if anybody with a knowledge of gnu assemblers for aarch64 could help me out.

I am using the Termux application, which is a Bash-like shell, installed the package Binutils, and using the gnu as assembler and ld linker to compile a short assembly program to open and read a small test disk file in the same path as my source. I am unable to open the file. If I examine the handle created by the open syscall the result is a negative number which is not right. I am not sure about register arguments for the svc call.

Here is my source file (rec1.s) written using nano, compiled using 'as -o rec1.o rec1.s', linked using ld -s -o rec1 rec1.o' and executed using './rec1'

.data
title: .ascii "Test disk open for aarch64\n"
filename: .ascii "test.txt"   // existing short text file
path: .ascii "../gas/"      // path where the file resides
err0: .ascii "Zero error\n"
errneg: .ascii "Negative error\n"

.bss
buffer: .space 100

.text
.global _start
_start:
  // title of program 
  mov x0,#1
  ldr x1,=title
  mov x2,#17
  mov w8,#64
  svc 0

  // open disk file 
  ldr x0,=path
  ldr x1,=filename
  mov x2,#0    // flag ?
  mov x3,#0666  // mode ?
  mov w8,#56
  svc 0             // x0 should have handle at this stage
  
  // check for valid handle
  cmp x0,#0    // branch if handle is zero
  beq error0
  blt errorneg
  mov x19,x0   // store handle in x19
  
  // read disk file 
  mov x0,x19
  ldr x1,=buffer
  mov x2,#10
  mov w8,#63
  svc 0

  // display buffer 
  mov x0,#1
  ldr x1,=buffer
  mov x2,#10
  mov w8,#64
  svc 0
  
  // close the file
  mov x0,19
  mov w8,#57
  svc 0
  b exit
  
  exit:
  mov x0,#0
  mov w8,#93
  svc 0
  
  error0:
  mov x0,#1
  ldr x1,=err0
  mov x2,#11
  mov w8,#64
  svc 0
  b exit
  
  errorneg:
  mov x0,#1
  ldr x1,=errneg
  mov x2,#15
  mov w8,#64
  svc 0
  b exit

The first argument to openat is not a string, but instead a file descriptor (returned by a previous call to openat ) or the value AT_FDCWD (-100 in Linux) to use the current directory.

The 4th argument is only used when creating files, so it can be omitted.

As Siguva points out the string passed to openat should be null-terminated.

filename: .asciz "../gas/test.txt"
  // open disk file 
  mov x0,#-100  // AT_FDCWD
  ldr x1,=filename
  mov x2,#0     // O_RDONLY
  mov w8,#56    // openat
  svc 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM