// File: /workspace/github_c_makefile_star2_O1/dwelch67[P]lc3/lc3dis.c
// Function: main
int main ( int argc, char *argv[] )
{
    unsigned int ra;
    unsigned int addr;
    unsigned int inst;
    unsigned int rd,rs1,rs2,simm,dest;
    FILE *fp;

    if(argc<2)
    {
        fprintf(stderr,".hex file not specified\n");
        return(1);
    }

    memset(mem,0xFF,sizeof(mem));
    memset(mem_hit,0x00,sizeof(mem_hit));

    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        fprintf(stderr,"Error opening file [%s]\n",argv[1]);
        return(1);
    }
    if(readhex(fp)) return(1);
    fclose(fp);

    for(addr=0;addr<=RAMMASK;addr++)
    {
        if(mem_hit[addr])
        {
            inst=mem[addr];
            printf("0x%04X: 0x%04X ",addr,inst);
            switch(inst&0xF000)
            {
                case 0x0000:
                {
                    printf("br");
                    if(inst&0x0800) printf("n");
                    if(inst&0x0400) printf("z");
                    if(inst&0x0200) printf("p");
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf(" 0x%04X",dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x1000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("add r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("add r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x2000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ld r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x3000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("st r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x4000:
                {
                    if(inst&0x0800)
                    {
                        simm=sext11(inst);
                        dest=(addr+1+simm)&0xFFFF;
                        printf("jsr 0x%04X",dest);
                        if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                        else            printf(" ; +%u",simm);
                    }
                    else
                    {
                        rd=(inst>>6)&7;
                        printf("jsrr r%u",rd);
                    }
                    break;
                }
                case 0x5000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("and r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("and r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x6000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("ldr r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x7000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("str r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x8000:
                {
                    printf("rti");
                    break;
                }
                case 0x9000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    printf("not r%u,r%u",rd,rs1);
                    break;
                }
                case 0xA000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ldi r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xB000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("sti r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xC000:
                {
                    printf("ret");
                    break;
                }
                case 0xD000:
                {
                    printf("undefined");
                    break;
                }
                case 0xE000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("lea r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xF000:
                {
                    rd=inst&0xFF;
                    printf("trap 0x%02X",rd);
                    break;
                }
            }


            printf("\n");
        }
    }



    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x1, %edi
jle 0x180a
movq %rsi, %r15
leaq 0x2daa(%rip), %rbx      # 0x4090
movl $0x20000, %edx          # imm = 0x20000
movq %rbx, %rdi
movl $0xff, %esi
callq 0x1070
leaq 0x22d91(%rip), %rdi     # 0x24090
movl $0x10000, %edx          # imm = 0x10000
xorl %esi, %esi
callq 0x1070
movq 0x8(%r15), %rdi
leaq 0xd47(%rip), %rsi       # 0x205d
callq 0x10a0
testq %rax, %rax
je 0x1811
movq %rax, %r12
movq %rax, %rdi
callq 0x1834
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x1822
movq %r12, %rdi
callq 0x1050
leaq 0xcb7(%rip), %r14       # 0x2004
xorl %r12d, %r12d
leaq 0x22d39(%rip), %r15     # 0x24090
cmpb $0x0, (%r12,%r15)
je 0x17f2
movzwl (%rbx), %r13d
leaq 0xd0c(%rip), %rdi       # 0x2079
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
movl %r13d, %eax
shrl $0xc, %eax
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
leaq 0xcf9(%rip), %rdi       # 0x2089
xorl %eax, %eax
callq 0x1060
btl $0xb, %r13d
jae 0x13a8
movl $0x6e, %edi
callq 0x1030
btl $0xa, %r13d
jae 0x13b9
movl $0x7a, %edi
callq 0x1030
btl $0x9, %r13d
jae 0x13ca
movl $0x70, %edi
callq 0x1030
andl $0x1ff, %r13d           # imm = 0x1FF
cmpl $0x100, %r13d           # imm = 0x100
movl $0xfe00, %r15d          # imm = 0xFE00
movl $0x0, %eax
cmovbl %eax, %r15d
orl %r13d, %r15d
leal (%r12,%r15), %eax
incl %eax
movzwl %ax, %esi
leaq 0xce3(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1060
cmpl $0x100, %r13d           # imm = 0x100
jae 0x1790
leaq 0xc7e(%rip), %rdi       # 0x2093
movl %r15d, %esi
jmp 0x1712
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %r13d           # imm = 0x1FF
xorl %eax, %eax
cmpl $0x100, %r13d           # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%r13), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %edx
addl $0xfffffe01, %edx       # imm = 0xFFFFFE01
andl %ecx, %edx
leaq 0xd11(%rip), %rdi       # 0x2176
jmp 0x16f8
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %r13d           # imm = 0x1FF
xorl %eax, %eax
cmpl $0x100, %r13d           # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%r13), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %edx
addl $0xfffffe01, %edx       # imm = 0xFFFFFE01
andl %ecx, %edx
leaq 0xca7(%rip), %rdi       # 0x2159
jmp 0x16f8
btl $0xb, %r13d
jb 0x1738
shrl $0x6, %r13d
andl $0x7, %r13d
leaq 0xc14(%rip), %rdi       # 0x20e5
movl %r13d, %esi
jmp 0x1564
leaq 0xc88(%rip), %rdi       # 0x2168
jmp 0x172c
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
shrl $0x6, %r13d
andl $0x7, %r13d
leaq 0xc41(%rip), %rdi       # 0x213e
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x17e8
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %r13d           # imm = 0x1FF
xorl %eax, %eax
cmpl $0x100, %r13d           # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%r13), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %edx
addl $0xfffffe01, %edx       # imm = 0xFFFFFE01
andl %ecx, %edx
leaq 0xb6a(%rip), %rdi       # 0x20be
jmp 0x16f8
movzbl %r13b, %esi
leaq 0xc21(%rip), %rdi       # 0x2185
xorl %eax, %eax
callq 0x1060
jmp 0x17e8
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %r13d           # imm = 0x1FF
xorl %eax, %eax
cmpl $0x100, %r13d           # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%r13), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %edx
addl $0xfffffe01, %edx       # imm = 0xFFFFFE01
andl %ecx, %edx
leaq 0xb14(%rip), %rdi       # 0x20cc
jmp 0x16f8
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %r13d, %edx
shrl $0x6, %edx
andl $0x7, %edx
andl $0x3f, %r13d
xorl %eax, %eax
cmpw $0x20, %r13w
setb %al
shll $0x6, %eax
addl %r13d, %eax
addl $-0x40, %eax
movzwl %ax, %r15d
leaq 0xb35(%rip), %rdi       # 0x2126
jmp 0x168e
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %r13d, %edx
shrl $0x6, %edx
andl $0x7, %edx
testb $0x20, %r13b
jne 0x179d
andl $0x7, %r13d
leaq 0xa91(%rip), %rdi       # 0x20ae
jmp 0x1646
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %r13d, %edx
shrl $0x6, %edx
andl $0x7, %edx
testb $0x20, %r13b
jne 0x17c0
andl $0x7, %r13d
leaq 0xabc(%rip), %rdi       # 0x2102
movl %r13d, %ecx
jmp 0x17e1
leaq 0xb17(%rip), %rdi       # 0x216c
jmp 0x172c
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %r13d, %edx
shrl $0x6, %edx
andl $0x7, %edx
andl $0x3f, %r13d
xorl %eax, %eax
cmpw $0x20, %r13w
setb %al
shll $0x6, %eax
addl %r13d, %eax
addl $-0x40, %eax
movzwl %ax, %r15d
leaq 0xa84(%rip), %rdi       # 0x2112
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1060
cmpw $0x20, %r13w
jb 0x1708
movl $0x10000, %esi          # imm = 0x10000
subl %r15d, %esi
leaq 0x9de(%rip), %rdi       # 0x208c
jmp 0x1712
movl %r13d, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %r13d           # imm = 0x1FF
xorl %eax, %eax
cmpl $0x100, %r13d           # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%r13), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %edx
addl $0xfffffe01, %edx       # imm = 0xFFFFFE01
andl %ecx, %edx
leaq 0xa52(%rip), %rdi       # 0x214a
xorl %eax, %eax
callq 0x1060
cmpl $0x100, %r13d           # imm = 0x100
jae 0x169f
leaq 0x984(%rip), %rdi       # 0x2093
movl %r15d, %esi
xorl %eax, %eax
callq 0x1060
leaq 0x22970(%rip), %r15     # 0x24090
jmp 0x17e8
leaq 0xa0e(%rip), %rdi       # 0x213a
xorl %eax, %eax
callq 0x1060
jmp 0x17e8
andl $0x7ff, %r13d           # imm = 0x7FF
xorl %eax, %eax
cmpl $0x400, %r13d           # imm = 0x400
setb %al
shll $0xb, %eax
leal (%rax,%r13), %r15d
addl $0xfffff800, %r15d      # imm = 0xFFFFF800
orl %r13d, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r15d
leal (%r12,%rax), %esi
addl $0xfffff801, %esi       # imm = 0xFFFFF801
andl %ecx, %esi
leaq 0x963(%rip), %rdi       # 0x20da
xorl %eax, %eax
callq 0x1060
cmpl $0x400, %r13d           # imm = 0x400
jae 0x169f
jmp 0x1708
movl $0x10000, %esi          # imm = 0x10000
subl %r15d, %esi
jmp 0x16a7
andl $0x1f, %r13d
xorl %eax, %eax
cmpw $0x10, %r13w
setb %al
shll $0x5, %eax
addl %r13d, %eax
addl $-0x20, %eax
movzwl %ax, %ecx
leaq 0x8dc(%rip), %rdi       # 0x209a
jmp 0x17e1
andl $0x1f, %r13d
xorl %eax, %eax
cmpw $0x10, %r13w
setb %al
shll $0x5, %eax
addl %r13d, %eax
addl $-0x20, %eax
movzwl %ax, %ecx
leaq 0x90d(%rip), %rdi       # 0x20ee
xorl %eax, %eax
callq 0x1060
movl $0xa, %edi
callq 0x1030
incq %r12
addq $0x2, %rbx
cmpq $0x10000, %r12          # imm = 0x10000
jne 0x1357
xorl %eax, %eax
jmp 0x1822
callq 0x1103
jmp 0x181d
addq $0x8, %r15
movq %r15, %rdi
callq 0x10e0
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)