1

I've wrote an ARM assembly program for the Raspberry pi 2, it takes the numbers:

a = 2
b = 3
c = 4
x = 5
y = 6

z = a*x*x+b*x*y+c*y*y

here is what it looks like:

.global _start
_start:
    MOV R1, #a
    MOV R2, #b
    MOV R3, #c
    MOV R4, #x
    MOV R5, #y
    @z = a*x*x+b*x*y+c*y*y  

    @R1 = a*x*x = 2*5*5 = 50
    MUL R1, R4, R1
    MUL R1, R4, R1
    @R2 = b*x*y = 3*5*6 = 90
    MUL R2, R4, R2
    MUL R2, R5, R2
    @R3 = c*y*y = 4*6*6 = 144
    MUL R3, R5, R3
    MUL R3, R5, R3

    @ R2 = R1 + R2 + R3 OR
    @ z = (a*x*x)+(b*x*y)+(c*y*y) = 50 + 90 + 144 = 284
    ADD R1, R2, R1
    ADD R0, R1, R3
_exit:
    MOV R7, #1
    SWI 0
.data
.equ a, 2
.equ b, 3
.equ c, 4
.equ x, 5
.equ y, 6

However when I compile and run the program and type 'echo $?' I get 28 as the output instead of the full 284, why is this?

James
  • 1,259
  • 1
  • 10
  • 21
  • Because [a child's exit status is an 8-bit value](http://stackoverflow.com/questions/33288356/c-retrieving-a-childs-exit-status-that-is-larger-than-8-bits) – Raymond Chen Feb 22 '16 at 01:36

1 Answers1

3

Using echo $? Will return the error code mod 256 so what you are returning is 284 % 256 = 28. U should instead use the printf function to print to the console

Nick M
  • 110
  • 5
  • Okay.. You've lost me, this is my first ARM program. Isn't printf a C function? – James Feb 21 '16 at 18:12
  • Yes, but remember that c and assembly are very closely tied so from assembly you can call certain native c functions such as printf – Nick M Feb 21 '16 at 18:13