// File: /workspace/github_c_makefile_star2_O1/dwelch67[P]lc3/lc3asm.c
// Function: main
int main ( int argc, char *argv[] )
{
    int ret;
    unsigned int ra;
    unsigned int rb;
    unsigned int inst;
    unsigned int inst2;

    if(argc!=2)
    {
        printf("lc3asm filename\n");
        return(1);
    }

    fpin=fopen(argv[1],"rt");
    if(fpin==NULL)
    {
        printf("Error opening file [%s]\n",argv[1]);
        return(1);
    }
    ret=assemble();
    fclose(fpin);

    if(ret) return(1);
    for(ra=0;ra<nlabs;ra++)
    {
        printf("label%04u: [0x%08X] [%s] %u\n",ra,lab_struct[ra].addr,lab_struct[ra].name,lab_struct[ra].type);

        if(lab_struct[ra].type)
        {
            for(rb=0;rb<nlabs;rb++)
            {
                if(lab_struct[rb].type) continue;
                if(strcmp(lab_struct[ra].name,lab_struct[rb].name)==0)
                {
                    rx=lab_struct[rb].addr;
                    inst=mem[lab_struct[ra].addr];

                    if((inst&0xF000)==0x0000)
                    {
                        //br[n][z][p]
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: branch too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0x2000)
                    {
                        //ld
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: ld too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xA000)
                    {
                        //ldi
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: ldi too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF800)==0x4800)
                    {
                        //jsr
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset11(rd))
                        {
                            printf("<%u> Error: jsr too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x07FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xE000)
                    {
                        //lea
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: lea too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0x3000)
                    {
                        //st
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: st too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xB000)
                    {
                        //sti
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: sti too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }

                    if(lab_struct[ra].type==1)
                    {
                        printf("<%u> Error: internal error, unknown instruction 0x%08X\n",lab_struct[ra].line,inst);
                        return(1);
                    }
                    mem[lab_struct[ra].addr]=inst;
                    break;
                }
            }
            if(rb<nlabs) ; else
            {
                printf("<%u> Error: unresolved label\n",lab_struct[ra].line);
                return(1);
            }
        }
    }
    sprintf(newline,"%s.hex",argv[1]);
    fpout=fopen(newline,"wt");
    if(fpout==NULL)
    {
        printf("Error creating file [%s]\n",newline);
        return(1);
    }

    for(ra=0;ra<=ADDMASK;ra++)
    {
        curradd=0x3000+ra;
        if(mark[ra]&0x80000000)
        {
            printf("0x%04X: 0x%04X\n",curradd,mem[ra]);

            rb=0x02;
            rb+=(curradd>>8)&0xFF;
            rb+=(curradd>>0)&0xFF;
            rb+=0x00;
            rb+=(mem[ra]>> 8)&0xFF;
            rb+=(mem[ra]>> 0)&0xFF;
            rb=(-rb)&0xFF;
            fprintf(fpout,":%02X%04X%02X%04X%02X\n",0x02,curradd&0xFFFF,0x00,mem[ra],rb);
        }
    }
    fclose(fpout);
    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
jne 0x3032
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
leaq 0x169e(%rip), %rsi      # 0x4278
callq 0x10b0
movq %rax, 0x543f1a(%rip)    # 0x546b00
testq %rax, %rax
je 0x3040
callq 0x186b
movl %eax, %r15d
movq 0x543f02(%rip), %rdi    # 0x546b00
callq 0x1050
movl $0x1, %eax
testl %r15d, %r15d
jne 0x306e
movq %rbx, -0x40(%rbp)
cmpl $0x0, 0x4c3ed4(%rip)    # 0x4c6af0
je 0x2f51
leaq 0x3ec7(%rip), %r14      # 0x6af0
leaq 0x1664(%rip), %rdi      # 0x4294
xorl %esi, %esi
imulq $0x4c, %rsi, %rbx
leaq (%r14,%rbx), %rcx
movl 0x40(%rbx,%r14), %edx
movl 0x48(%rbx,%r14), %r8d
movq %rsi, -0x48(%rbp)
movq %rcx, -0x30(%rbp)
xorl %eax, %eax
callq 0x1060
movl 0x48(%rbx,%r14), %eax
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x2f2d
movl 0x4c3e87(%rip), %ebx    # 0x4c6af0
testq %rbx, %rbx
setne %r12b
je 0x3054
movq %rbx, %r13
negq %r13
movl $0x1, %r14d
leaq 0x3e67(%rip), %r15      # 0x6af0
cmpl $0x0, 0x48(%r15)
jne 0x2ca0
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x2cc0
cmpq %rbx, %r14
setb %r12b
addq $0x4c, %r15
leaq (%r14,%r13), %rax
incq %rax
incq %r14
cmpq $0x1, %rax
jne 0x2c89
jmp 0x3054
movl 0x40(%r15), %ecx
movl %ecx, 0x3e06(%rip)      # 0x6ad0
movq -0x30(%rbp), %r8
movl 0x40(%r8), %eax
leaq 0x4c3e27(%rip), %rdx    # 0x4c6b00
movl (%rdx,%rax,4), %edx
testl $0xf000, %edx           # imm = 0xF000
jne 0x2d2b
leal 0x3000(%rax), %esi
movl %esi, 0x3de8(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3dd4(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2d1a
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30f2
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
movl -0x34(%rbp), %esi
incl %esi
movl %esi, 0x48(%r8)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x2000, %esi           # imm = 0x2000
jne 0x2d7d
leal 0x3000(%rax), %esi
movl %esi, 0x3d91(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3d7d(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2d71
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30a1
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl 0x48(%r8)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xa000, %esi           # imm = 0xA000
jne 0x2dcf
leal 0x3000(%rax), %esi
movl %esi, 0x3d3f(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3d2b(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2dc3
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30ae
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl 0x48(%r8)
movl %edx, %esi
andl $0xf800, %esi           # imm = 0xF800
cmpl $0x4800, %esi           # imm = 0x4800
jne 0x2e1a
leal 0x3000(%rax), %esi
movl %esi, 0x3ced(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3cd9(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0xa, %edi
andl $0x3f, %edi
je 0x2e0e
cmpl $0x3f, %edi
jne 0x30bb
andl $0x7ff, %esi            # imm = 0x7FF
orl %esi, %edx
incl 0x48(%r8)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xe000, %esi           # imm = 0xE000
jne 0x2e6c
leal 0x3000(%rax), %esi
movl %esi, 0x3ca2(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3c8e(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2e60
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30c8
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl 0x48(%r8)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x3000, %esi           # imm = 0x3000
jne 0x2ebe
leal 0x3000(%rax), %esi
movl %esi, 0x3c50(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3c3c(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2eb2
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30d5
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl 0x48(%r8)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xb000, %esi           # imm = 0xB000
jne 0x2f0e
leal 0x3000(%rax), %esi
movl %esi, 0x3bfe(%rip)      # 0x6ad8
subl %eax, %ecx
addl $0xffffcfff, %ecx       # imm = 0xFFFFCFFF
movl %ecx, 0x3bec(%rip)      # 0x6ad4
movl %ecx, %esi
shrl $0x8, %esi
testb %sil, %sil
je 0x2f02
movzbl %sil, %esi
cmpl $0xff, %esi
jne 0x30e2
andl $0x1ff, %ecx            # imm = 0x1FF
orl %ecx, %edx
incl 0x48(%r8)
cmpl $0x1, 0x48(%r8)
je 0x308d
leaq 0x4c3be0(%rip), %rcx    # 0x4c6b00
movl %edx, (%rcx,%rax,4)
testb $0x1, %r12b
je 0x3054
movq -0x48(%rbp), %rsi
incq %rsi
movl 0x4c3bb6(%rip), %eax    # 0x4c6af0
cmpq %rax, %rsi
leaq 0x3bac(%rip), %r14      # 0x6af0
leaq 0x1349(%rip), %rdi      # 0x4294
jb 0x2c32
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x3360(%rip), %r14      # 0x62c0
leaq 0x13a0(%rip), %rsi      # 0x4307
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0x1396(%rip), %rsi      # 0x430e
movq %r14, %rdi
callq 0x10b0
movq %rax, 0x543b89(%rip)    # 0x546b10
testq %rax, %rax
je 0x307d
movl $0x3000, %ebx           # imm = 0x3000
leaq 0x503b64(%rip), %r12    # 0x506b00
leaq 0x4c3b5d(%rip), %r13    # 0x4c6b00
leaq 0x1391(%rip), %r15      # 0x433b
xorl %r14d, %r14d
movl %ebx, 0x4c3b41(%rip)    # 0x4c6af4
cmpl $0x0, (%r14,%r12)
jns 0x3013
movl (%r14,%r13), %edx
leaq 0x1366(%rip), %rdi      # 0x432b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1060
movl 0x4c3b20(%rip), %eax    # 0x4c6af4
movl %eax, %ecx
shrl $0x8, %ecx
movl (%r14,%r13), %r9d
movl %r9d, %edx
shrl $0x8, %edx
addl %eax, %ecx
addl %r9d, %ecx
addl %edx, %ecx
movl $0xfe, %edx
subl %ecx, %edx
movzbl %dl, %edx
movq 0x543b15(%rip), %rdi    # 0x546b10
movzwl %ax, %ecx
movl %edx, (%rsp)
movq %r15, %rsi
movl $0x2, %edx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10a0
addq $0x4, %r14
incl %ebx
cmpq $0x40000, %r14          # imm = 0x40000
jne 0x2fad
movq 0x543ae7(%rip), %rdi    # 0x546b10
callq 0x1050
xorl %eax, %eax
jmp 0x306e
leaq 0x1319(%rip), %rdi      # 0x4352
callq 0x1040
jmp 0x3069
movq 0x8(%rbx), %rsi
leaq 0x1230(%rip), %rdi      # 0x427b
xorl %eax, %eax
callq 0x1060
jmp 0x3069
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1287(%rip), %rdi      # 0x42e9
xorl %eax, %eax
callq 0x1060
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x128d(%rip), %rdi      # 0x4311
leaq 0x3235(%rip), %rsi      # 0x62c0
jmp 0x304b
movl 0x44(%r8), %esi
leaq 0x1219(%rip), %rdi      # 0x42b1
xorl %eax, %eax
callq 0x1060
jmp 0x3069
movl 0x44(%r8), %esi
leaq 0x110d(%rip), %rdi      # 0x41b9
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x111d(%rip), %rdi      # 0x41d6
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x10d6(%rip), %rdi      # 0x419c
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x1126(%rip), %rdi      # 0x41f9
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x1136(%rip), %rdi      # 0x4216
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x1146(%rip), %rdi      # 0x4233
jmp 0x3062
movl 0x44(%r8), %esi
leaq 0x105b(%rip), %rdi      # 0x4158
jmp 0x3062