EXC_BAD_ACCESS error/crash when trying to do "add eax, [esi]" in x86 assembly

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;
}



I ran your code on my Intel machine and, while it doesn’t crash, it produces incorrect results:

total using c++ = 355
total shirts using asm = 527584

I suspect that’s because you posted the “working but wrong” version rather than the “correct but crashes” version (-:

The error you’re hitting is because you’re accessing memory out of bounds. I’m hardly an expert on Intel assembly language but the most obvious problem here is that you’re using register esi. Apple dropped 32-bit Intel support a while back, so any modern machine only runs 64-bit code. That means you need to use the 64-bit variant of this register, that is, rsi. So this line:

lea esi, [b]

produces a truncated value. If I set a breakpoint after that line, compare the value in esi:

(lldb) p/a $esi 
(unsigned int) $0 = 0x000080b0

and the actual address of b:

(lldb) p/a &b[0][0]
(int *) $2 = 0x00000001000080b0 b

This truncation means that, when you attempt to access the address, you hit the 4 GiB of unmapped memory at the bottom of your address space and that triggers this crash.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

EXC_BAD_ACCESS error/crash when trying to do "add eax, [esi]" in x86 assembly
 
 
Q