简体   繁体   中英

Arm Assembly Floating Point

Hi I was wondering if anyone here could point me in the direction of solving this problem.

Write and run an ARM VFP assembly program to calculate the volume of a sphere: 4(pi*r^3)/3 if r = 25.5. Your TI Launchpad supports floating point operations, however it is switched off at reset (so you must turn it on!)

It was a question from my textbook ARM Assembly Language Programming & Architecture

So far I have Written this code in order to solve the equation and was wondering how I could go about outputting this value and making sure I have enabled the floating point.

 ; have to enable The CPAC register in order to use floating point: offset 0xD88


VMOV.F32 S0, #25.5  ;SO NOW HOLDS THE CONSTANT 25.5
VMOV.F32 S1, #3.14  ;S1 NOW HOLDS THE CONSTANT 3.14
VMOV.F32 S2, #4     ;S2 NOW HOLDS THE CONSTANT 4
VMOV.F32 S3, #3     ;S3 NOW HOLDS THE CONSTANT 3

VMUL.F32 S4,S0,S0   ;S4 NOW HOLDS THE VALUE R^2
VMUL.F32 S4,S4,S0   ;S4 NOW HOLDS THE VALUE R^3

VMUL.F32 S5,S4,S1   ;S5 NOW HOLDS THE VALUE 3.14*R^3

VMUL.F32 S6,S5,S2   ;S6 NOW HOLDS THE VALUE 4(3.14*R^3)

VMUL.F32 S7,S6,S3   ;S7 NOW HOLDS THE VALUE (4(3.14*R^3))/3

; v = 69455.9
; this number V has to be converted to IEE in order to be output??? = 0100 0111 1000 0111 1010 0111 1111 0011

some code I left laying around for a ti launchpad.

.thumb
.thumb_func
.globl fpu_init
fpu_init:
    ;@ basically straight from ti datasheet
    ;@ set bits in CPAC
    ldr r0,=0xE000ED88
    ldr r1,[r0]
    ldr r2,=0xF00000
    orr r1,r2
    str r1,[r0]
    dsb
    isb
    bx lr

.thumb_func
.globl fun
fun:
    vmov s0,r0
    vcvt.f32.u32 s0,s0
    vmov s1,r1
    vcvt.f32.u32 s1,s1
    vmul.f32 s0,s0,s1
    vcvt.u32.f32 s0,s0
    vmov r0,s0
    bx lr


.thumb_func
.globl ffun
ffun:
    vmov s0,r0
    vcvt.f32.u32 s0,s0
    vmov s1,r1
    vcvt.f32.u32 s1,s1
    vmul.f32 s0,s0,s1
    vmov r0,s0
    bx lr

the latter is not the same as yours but pretty sure one converts the result back to integer the other simply returns the binary pattern for the float (simply moves s0 bits to r0).

so you can wrap your function with what you need to assemble it then call it from a C function store your result in r0

something like this

.thumb_func
.globl FUN
FUN:
  your code
  vmov r0,s7
  bx lr

In C prototype as

unsigned int FUN ( void );

and then "just" print the result (grin) or use a debugger and inspect r0 or whatever you do to see stuff currently (I print the 32 bit register as hex out the uart 0x12345678 in the register prints on the terminal as 12345678).

you can feed the assembly file (fun.s) directly into gcc if you dont want or your build system cant handle calling the assembler (or assembly by hand arm-whatever-as -mthumb -mfpu=vfp fun.s -o fun.o) then link that in and then call the function.

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