1 // Copyright 2013, ARM Limited 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // 7 // * Redistributions of source code must retain the above copyright notice, 8 // this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above copyright notice, 10 // this list of conditions and the following disclaimer in the documentation 11 // and/or other materials provided with the distribution. 12 // * Neither the name of ARM Limited nor the names of its contributors may be 13 // used to endorse or promote products derived from this software without 14 // specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 #include "examples.h" 28 29 #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0])) 30 #define BUF_SIZE (4096) 31 #define __ masm-> 32 33 void GenerateSumArray(MacroAssembler* masm) { 34 // uint32_t sum_array(uint8_t* array, uint32_t size) 35 // Argument locations: 36 // array (pointer) -> x0 37 // size -> x1 38 39 Label loop, end; 40 41 __ Mov(x2, x0); 42 __ Mov(w0, 0); 43 44 // There's nothing to do if the array is empty. 45 __ Cbz(w1, &end); 46 47 // Go through the array and sum the elements. 48 __ Bind(&loop); 49 50 __ Ldrb(w3, MemOperand(x2, 1, PostIndex)); // w3 = *(x2++) 51 __ Add(w0, w0, w3); 52 53 __ Sub(w1, w1, 1); 54 __ Cbnz(w1, &loop); 55 56 __ Bind(&end); 57 __ Ret(); 58 } 59 60 61 #ifndef TEST_EXAMPLES 62 int main(void) { 63 // Create and initialize the assembler and the simulator. 64 byte assm_buf[BUF_SIZE]; 65 MacroAssembler masm(assm_buf, BUF_SIZE); 66 Decoder decoder; 67 Simulator simulator(&decoder); 68 69 // Generate the code for the example function. 70 Label sum_array; 71 masm.Bind(&sum_array); 72 GenerateSumArray(&masm); 73 masm.FinalizeCode(); 74 75 // Run the example function. 76 uint8_t data[] = { 2, 45, 63, 7, 245, 38 }; 77 uintptr_t data_addr = reinterpret_cast<uintptr_t>(data); 78 simulator.set_xreg(0, data_addr); 79 simulator.set_xreg(1, ARRAY_SIZE(data)); 80 simulator.RunFrom(sum_array.target()); 81 82 unsigned int i; 83 for (i = 0; i < ARRAY_SIZE(data) - 1; ++i) { 84 printf("%d + ", data[i]); 85 } 86 printf("%d = %d\n", data[i], simulator.wreg(0)); 87 88 return 0; 89 } 90 #endif 91