0

i have a programm in armv6 assembly which calculates the result of (x +y)^2

this code doesn't work, and returns: "Unsupported ARM syscall: 0xffffffdf"

.global _start
.text
_start:
    MOV r0, #4
    MOV r1, #5
    MOV r7, #1
    BL calc
    SWI #0

calc:
    ADD r7, r0, r1
    MUL R0, R7, R7
    MOV PC, LR

but this one is slightly edited and works (or it doesn't?):

.global _start
.text
_start:
   MOV r0, #4
   MOV r1, #5
   MOV r7, #1
   BL calc
   BL exit

calc:
   ADD r7, r0, r1
   MUL R0, R7, R7
   MOV PC, LR

exit:
   LDR r7, =1
   SWI 0

can anyone please tell me why the first code is not working? is the second one even valid?

immi0815
  • 39
  • 3
  • 3
    What operating system are you programming for? – fuz Jan 25 '23 at 09:20
  • 2
    What do you think the value of `r7` is throughout the program? – Timothy Baldwin Jan 25 '23 at 09:39
  • 1
    If this is a Linux system call, then `r7` selects the system call number. `_exit()` is system call number 1. Your second code executes `swi 0` with `r7` containing the value 1, so it works. In the first code, `r7` does not contain 1 when `swi 0` executes, because the `calc` function modified it. – Nate Eldredge Jan 25 '23 at 15:45
  • If this is Linux, run `strace ./a.out` to trace/decode the system calls your code makes, to help you understand Nate's comment. Oh, you already have an error message about a bad system-call number, but strangely it's showing it as `0xffffffdf` not `9`, the call-number you actually used in R7. – Peter Cordes Jan 25 '23 at 15:47
  • 1
    @PeterCordes 9 is the first system call, `0xffffffdf` is the third system call. – Timothy Baldwin Jan 25 '23 at 16:49
  • 1
    If this is `qemu-arm` running under linux (as the error message suggests), pass the `-strace` option to trace system calls. – Timothy Baldwin Jan 25 '23 at 16:53
  • @TimothyBaldwin: Oh right, after `SWI` returns (with an error code in R0), it loops since execution falls into `exit` with LR still set. – Peter Cordes Jan 25 '23 at 17:12

1 Answers1

1

First set r0 and r1:

 MOV r0, #4
 MOV r1, #5

Setting r7 here has no effect:

 MOV r7, #1

Call calc:

 BL calc

calc:
 ADD r7, r0, r1
 MUL R0, R7, R7
 MOV PC, LR

r7 is now 4 + 5 = 9.

Call system call number 9, which is link:

 SWI 0

link requires 2 arguments which are pointers to strings, since neihter 81 nor 5 are valid pointers it returns the error -EFAULT = -14 in r0.

calc:
 ADD r7, r0, r1
 MUL R0, R7, R7
 MOV PC, LR

r7 is now -14 + 5 = -9 = 0xfffffff7.

Call system call number 0xfffffff7, which does not exist:

 SWI 0

The error -ENOSYS = -38 is returned in r0.

calc:
 ADD r7, r0, r1
 MUL R0, R7, R7
 MOV PC, LR

r7 is now -38 + 5 = -33 = 0xffffffdf.

Call system call number 0xffffffdf, which does not exist:

 SWI 0

And this repeats indefinitely.

The correct program sets r7 to 1 before SWI 0 so executes the exit system call.

Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23