in ARMv6 assembly one code is not working, but the modified version works. Why?

127 Views Asked by At

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?

1

There are 1 best solutions below

0
Timothy Baldwin On

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.