// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
pushq $0x1
popq %rbx
cmpl $0x2, %edi
jne 0x2caf
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x8fb(%rip), %rsi       # 0x3268
callq 0x10b0
movq %rax, 0x543187(%rip)    # 0x545b00
testq %rax, %rax
je 0x2cbd
callq 0x181c
movl %eax, %r15d
movq 0x54316f(%rip), %rdi    # 0x545b00
callq 0x1050
testl %r15d, %r15d
jne 0x2ce3
leaq 0x314a(%rip), %r12      # 0x5af0
xorl %esi, %esi
movl 0x4c3142(%rip), %eax    # 0x4c5af0
cmpq %rax, %rsi
jae 0x2cf4
imulq $0x4c, %rsi, %r15
leaq (%r12,%r15), %r13
movl 0x40(%r15,%r12), %edx
movl 0x48(%r15,%r12), %r8d
leaq 0x8b4(%rip), %rdi       # 0x3284
movq %rsi, -0x38(%rbp)
movq %r13, %rcx
xorl %eax, %eax
callq 0x1060
movl 0x48(%r15,%r12), %eax
testl %eax, %eax
je 0x2ca3
movl %eax, -0x2c(%rbp)
movq %r12, %r15
movl 0x4c30f8(%rip), %r12d   # 0x4c5af0
subq $0x1, %r12
jb 0x2cd1
cmpl $0x0, 0x48(%r15)
jne 0x2a18
movq %r13, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x2a1e
addq $0x4c, %r15
jmp 0x29f8
movl 0x40(%r15), %ecx
movl %ecx, 0x30a8(%rip)      # 0x5ad0
movl 0x40(%r13), %eax
leaq 0x4c30cd(%rip), %rdx    # 0x4c5b00
movl (%rdx,%rax,4), %edx
testw $0xf000, %dx            # imm = 0xF000
je 0x2a4a
leaq 0x30ac(%rip), %r12      # 0x5af0
movl -0x2c(%rbp), %r8d
jmp 0x2a9a
leal 0x3000(%rax), %esi
movl %esi, 0x3082(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x306e(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
leaq 0x307b(%rip), %r12      # 0x5af0
movl -0x2c(%rbp), %r8d
je 0x2a8b
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x2e5f
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x2000, %esi           # imm = 0x2000
jne 0x2aef
leal 0x3000(%rax), %esi
movl %esi, 0x3022(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x300e(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2ae0
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x2dff
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xa000, %esi           # imm = 0xA000
jne 0x2b44
leal 0x3000(%rax), %esi
movl %esi, 0x2fcd(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x2fb9(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2b35
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x2e0f
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf800, %esi           # imm = 0xF800
cmpl $0x4800, %esi           # imm = 0x4800
jne 0x2b92
leal 0x3000(%rax), %esi
movl %esi, 0x2f78(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x2f64(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0xa, %edi
andl $0x3f, %edi
je 0x2b83
cmpl $0x3f, %edi
jne 0x2e1f
andl $0x7ff, %esi            # imm = 0x7FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xe000, %esi           # imm = 0xE000
jne 0x2be7
leal 0x3000(%rax), %esi
movl %esi, 0x2f2a(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x2f16(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2bd8
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x2e2f
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x3000, %esi           # imm = 0x3000
jne 0x2c3c
leal 0x3000(%rax), %esi
movl %esi, 0x2ed5(%rip)      # 0x5ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x2ec1(%rip)      # 0x5ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2c2d
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x2e3f
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r8d
movl %r8d, 0x48(%r13)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xb000, %esi           # imm = 0xB000
jne 0x2c8f
leal 0x3000(%rax), %esi
movl %esi, 0x2e80(%rip)      # 0x5ad8
subl %eax, %ecx
addl $0xffffcfff, %ecx       # imm = 0xFFFFCFFF
movl %ecx, 0x2e6e(%rip)      # 0x5ad4
movl %ecx, %esi
shrl $0x8, %esi
testb %sil, %sil
je 0x2c80
movzbl %sil, %esi
cmpl $0xff, %esi
jne 0x2e4f
andl $0x1ff, %ecx            # imm = 0x1FF
orl %ecx, %edx
incl %r8d
movl %r8d, 0x48(%r13)
cmpl $0x1, %r8d
je 0x2de8
leaq 0x4c2e60(%rip), %rcx    # 0x4c5b00
movl %edx, (%rcx,%rax,4)
movq -0x38(%rbp), %rsi
incq %rsi
jmp 0x29a8
leaq 0x68c(%rip), %rdi       # 0x3342
callq 0x1040
jmp 0x2ce3
movq 0x8(%r14), %rsi
leaq 0x5a3(%rip), %rdi       # 0x326b
xorl %eax, %eax
callq 0x1060
jmp 0x2ce3
movl 0x44(%r13), %esi
leaq 0x5fd(%rip), %rdi       # 0x32d9
xorl %eax, %eax
callq 0x1060
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rdx
leaq 0x25c1(%rip), %r14      # 0x52c0
leaq 0x5f1(%rip), %rsi       # 0x32f7
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0x5e7(%rip), %rsi       # 0x32fe
movq %r14, %rdi
callq 0x10b0
movq %rax, 0x542dea(%rip)    # 0x545b10
testq %rax, %rax
je 0x2dd5
movl $0x3000, %ebx           # imm = 0x3000
leaq 0x502dc5(%rip), %r13    # 0x505b00
pushq $0x2
popq %r12
xorl %r14d, %r14d
cmpq $0x40000, %r14          # imm = 0x40000
je 0x2dc2
movl %ebx, 0x4c2da3(%rip)    # 0x4c5af4
cmpl $0x0, (%r14,%r13)
jns 0x2dba
leaq 0x4c2da1(%rip), %r15    # 0x4c5b00
movl (%r14,%r15), %edx
leaq 0x5b1(%rip), %rdi       # 0x331b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1060
movl 0x4c2d7b(%rip), %eax    # 0x4c5af4
movl %eax, %ecx
shrl $0x8, %ecx
movl (%r14,%r15), %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 0x542d70(%rip), %rdi    # 0x545b10
movzwl %ax, %ecx
movl %edx, (%rsp)
leaq 0x57e(%rip), %rsi       # 0x332b
movl %r12d, %edx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10a0
incl %ebx
addq $0x4, %r14
jmp 0x2d42
movq 0x542d47(%rip), %rdi    # 0x545b10
callq 0x1050
xorl %ebx, %ebx
jmp 0x2ce3
leaq 0x525(%rip), %rdi       # 0x3301
leaq 0x24dd(%rip), %rsi      # 0x52c0
jmp 0x2cc8
movl 0x44(%r13), %esi
leaq 0x4ae(%rip), %rdi       # 0x32a1
xorl %eax, %eax
callq 0x1060
jmp 0x2ce3
movl 0x44(%r13), %esi
leaq 0x3a3(%rip), %rdi       # 0x31ad
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x3b0(%rip), %rdi       # 0x31ca
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x36a(%rip), %rdi       # 0x3194
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x3b3(%rip), %rdi       # 0x31ed
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x3bc(%rip), %rdi       # 0x3206
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x3c9(%rip), %rdi       # 0x3223
jmp 0x2cdc
movl 0x44(%r13), %esi
leaq 0x2e6(%rip), %rdi       # 0x3150
jmp 0x2cdc