Im currently using a M1 macbook pro, running xcode using rosetta 2 to be able to code x86 assembly and has worked well so far, however im running into a problem that only occurs on my machine when trying to find the total sum of the contents in an array. My main issue occurs when doing "add eax, [esi]", in the terminal a (11db) crash is returned and am unable to find a solution. (note what when i do "add eax, esi" it compiles but returns a large number which im assuming is a sum of every elements addresses")
the exact error im getting is: "EXC_BAD_ACCESS (code =1, address = 0x80b0 it also points to this line in the assembly file: -> 0x20001d52e <+462>: movl %eax, %r14d
#include <iostream>
using namespace std;
int b[4][4] = {10,20,30,40,20,10,40,30,5,15,20,25,30,25,20,15};
/*
___________________________________________
| small | medium | large | x-large |
red | 10 | 20 | 30 | 40 |
green | 20 | 10 | 40 | 30 |
blue | 5 | 15 | 20 | 25 |
black | 30 | 25 | 20 | 15 |
------------------------------------------
Int = 4 bits -> every index is a+0, a+4^n... so on
*/
int totalShirts, totalMed, totalBlue, total;
int i;
int main() {
//c++
for (int i =0; i < 4; i++){
for (int j =0; j <4; j++){
total += b[i][j];
}
}
cout << "total using c++ = " << total << endl;
/*
Questions (all must use loops)
a. display total number of (all) shirts
b. display total number of medium shirts
c. display total number of blue shirts
*/
__asm{
mov eax, 0;
mov i, 0;
lea esi, [b]
totalLoop:
cmp i, 16;
je done;
add eax, esi;
inc i;
add esi, 4;
jmp totalLoop;
done:
mov totalShirts, eax;
};
cout << "total shirts using asm = " << totalShirts << endl;
return 0;
}