// File: saumya-prakash[P]AssemblerEmulator/Emulator.cpp
// Function: Emulator::loader()
bool Emulator::loader()     // no size checking has been done now -> just simple loading
{    
    ifstream fi(obj_filename, ifstream::binary | ifstream::in);

    if(!fi) // ERROR in opening file
    {
        return false;
    }

    char waste;

    char aux[8] = "\0";
    for(unsigned i=0; i<8; ++i)
    {
        fi.read(aux+i, 1);
    }

    for(unsigned i=0; i<8; ++i)
    {
        if(aux[i]!=format_code[i])
        {
            cout<<i<<" ";
            cout<<"Unknown object file format"<<endl;
            return false;
        }
    }

    fi.read(&waste, 1);
    if(waste!='\n')
    {
        cout<<"Unknown onject file format"<<endl;
        return false; 
    }


    text_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<text_size; ++i)     // load text segment
    {
        mempory_space[i] = get_int(fi);
    }

    fi.read(&waste, 1);     // dump newline character
    fi.read(&waste, 1);     // dump newline character
    
    data_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<data_size; ++i)
    {
        mempory_space[i+data_lower] = get_int(fi);
    }


    PC = 0x00000000;     // set PC to 0x0
    A = 0x00000000;
    B = 0x00000000;
    data_upper = data_lower + data_size - 1;    // denotes upper boundary of data loaded
    SP = 0xff00;    // set SP to ?? NOT CONFIRMED


    fi.close();     // close the object file

    return true;    // loading successful
}

// Assembly:
subq $0x278, %rsp            # imm = 0x278
movq %rdi, 0x268(%rsp)
movq 0x268(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x4, %edi
movl $0x8, %esi
callq 0x7400
movq 0x28(%rsp), %rsi
movl %eax, %edx
leaq 0x60(%rsp), %rdi
callq 0x2360
movq 0x60(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x60(%rsp,%rax), %rdi
callq 0x2100
movb %al, 0x33(%rsp)
jmp 0x4af5
movb 0x33(%rsp), %al
testb $0x1, %al
jne 0x4aff
jmp 0x4b31
movb $0x0, 0x277(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x4e0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2090
jmp 0x4e27
leaq 0x47(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2110
movl $0x0, 0x40(%rsp)
cmpl $0x8, 0x40(%rsp)
jae 0x4b7a
movl 0x40(%rsp), %eax
leaq 0x47(%rsp,%rax), %rsi
leaq 0x60(%rsp), %rdi
movl $0x1, %edx
callq 0x22d0
jmp 0x4b6b
jmp 0x4b6d
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x4b4a
movl $0x0, 0x3c(%rsp)
cmpl $0x8, 0x3c(%rsp)
jae 0x4c29
movl 0x3c(%rsp), %eax
movsbl 0x47(%rsp,%rax), %eax
movl 0x3c(%rsp), %ecx
movl %ecx, %edx
leaq 0x5775(%rip), %rcx      # 0xa318
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x4c17
movl 0x3c(%rsp), %esi
movq 0x941a(%rip), %rdi      # 0xdfd0
callq 0x2300
movq %rax, 0x20(%rsp)
jmp 0x4bc2
movq 0x20(%rsp), %rdi
leaq 0x596f(%rip), %rsi      # 0xa53d
callq 0x21f0
jmp 0x4bd5
movq 0x93f4(%rip), %rdi      # 0xdfd0
leaq 0x5893(%rip), %rsi      # 0xa476
callq 0x21f0
movq %rax, 0x18(%rsp)
jmp 0x4bef
movq 0x18(%rsp), %rdi
movq 0x93c5(%rip), %rsi      # 0xdfc0
callq 0x2240
jmp 0x4c02
movb $0x0, 0x277(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x4e0c
jmp 0x4c19
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x4b82
leaq 0x60(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
movl $0x1, %edx
callq 0x22d0
jmp 0x4c3f
movsbl 0x4f(%rsp), %eax
cmpl $0xa, %eax
je 0x4c8b
movq 0x9380(%rip), %rdi      # 0xdfd0
leaq 0x583a(%rip), %rsi      # 0xa491
callq 0x21f0
movq %rax, 0x10(%rsp)
jmp 0x4c63
movq 0x10(%rsp), %rdi
movq 0x9351(%rip), %rsi      # 0xdfc0
callq 0x2240
jmp 0x4c76
movb $0x0, 0x277(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x4e0c
movq 0x28(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4e40
movl %eax, 0xc(%rsp)
jmp 0x4ca0
movq 0x28(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, 0x48(%rax)
leaq 0x60(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
movl $0x1, %edx
callq 0x22d0
jmp 0x4cc2
movl $0x0, 0x38(%rsp)
movq 0x28(%rsp), %rcx
movl 0x38(%rsp), %eax
cmpl 0x48(%rcx), %eax
jae 0x4d0e
movq 0x28(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4e40
movl %eax, 0x8(%rsp)
jmp 0x4ced
movl 0x8(%rsp), %edx
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x38(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x4cca
leaq 0x60(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
movl $0x1, %edx
callq 0x22d0
jmp 0x4d24
leaq 0x60(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
movl $0x1, %edx
callq 0x22d0
jmp 0x4d3a
movq 0x28(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4e40
movl %eax, 0x4(%rsp)
jmp 0x4d4f
movq 0x28(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, 0x4c(%rax)
leaq 0x60(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
movl $0x1, %edx
callq 0x22d0
jmp 0x4d71
movl $0x0, 0x34(%rsp)
movq 0x28(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x4c(%rcx), %eax
jae 0x4dc3
movq 0x28(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4e40
movl %eax, (%rsp)
jmp 0x4d9b
movl (%rsp), %edx
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x34(%rsp), %ecx
addl $0x400, %ecx            # imm = 0x400
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x4d79
movq 0x28(%rsp), %rax
movl $0x0, 0x28(%rax)
movl $0x0, 0x20(%rax)
movl $0x0, 0x24(%rax)
movl 0x4c(%rax), %ecx
addl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, 0x38(%rax)
movl $0xff00, 0x2c(%rax)     # imm = 0xFF00
leaq 0x60(%rsp), %rdi
callq 0x2070
jmp 0x4dfc
movb $0x1, 0x277(%rsp)
movl $0x1, 0x50(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2090
movb 0x277(%rsp), %al
andb $0x1, %al
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x58(%rsp), %rdi
callq 0x2390
nopw %cs:(%rax,%rax)