// File: /workspace/github_cpp_makefile_star2_O1/czmccrory[P]ManchesterBaby/assembler.cpp
// Function: Assembler::assembleLineFirstPass(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, int*)
void Assembler::assembleLineFirstPass(string* line, int* lineCounter)
{
	string bufferLine;

	string parsed = parse(line);

	if (parsed == "") // nothing useful left on line
	{
		return;
	}

	// if theres a "LABEL": add it to symbol table along with current line number as address
	if (parsed.find(":") != string::npos)
	{
		//cout << "Label found: " << parsed;
		string label = parsed.substr(0, parsed.length()-1);

		addVariableToSymbolTable(label, *lineCounter);

		parsed = parse(line);

	}

	// if "VAR", declareVar(next word)
	if (parsed == "VAR")
	{
		int var;
		stringstream ss;
		try 
		{
			ss << parse(line);
			ss >> var;
		}
		catch (AssemblerException &e)
		{
			cout << "\033[1;31mAssembler Error: \033[0m" << e.what() << endl;
			exit(0);
		}
    
		bufferLine = declareVariable(var);
		cout << "Machine Code: " << bufferLine << "\n" << endl;

		(*lineCounter)++;

	}
	else if(iSet.contains(parsed)) // recognised instruction
	{
		if (parsed != "STP" && parsed != "CMP" && parsed != "INC" && parsed != "DEC") // has an operand
		{
			try 
			{
				string instrOperand = parse(line);
				if (instrOperand.find("#") == string::npos) // if direct addressing do nothing
				{
					bufferLine = encodeInstructionAddress(parsed, instrOperand);
				}
				cout << "Machine Code: " << bufferLine << "\n" << endl;
			}
			catch (AssemblerException &e)
			{
				cout << "\033[1;31mAssembler Error: \033[0m" << e.what() << endl;
				exit(0);
			}
		}
		
		(*lineCounter) = (*lineCounter + 1);
	}
	else
	{
		throw AssemblerException("Unidentified Opcode, this Opcode is not part of the instruction set");
		exit(0);
	 }	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x60(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x28(%rsp), %rdi
movq %rsi, %rdx
callq 0x4474
leaq 0x343c(%rip), %rsi      # 0x7100
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4144
leaq 0x3476(%rip), %rsi      # 0x7153
leaq 0x28(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x3dca
leaq 0x28(%rsp), %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x110(%rsp), %rdi
xorl %edx, %edx
callq 0x2210
leaq 0xe0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x5ee4
movl (%rbx), %edx
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x47ac
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x3d6f
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x8(%rsp), %rdi
movq %r15, %rdx
callq 0x4474
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3da9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3dca
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x331b(%rip), %rsi      # 0x70ec
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x401d
leaq 0xc0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5ee4
leaq 0xb0(%rsp), %rsi
movq %r14, %rdi
callq 0x2b12
movl %eax, %ebp
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x3e39
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x21f0
testb %bpl, %bpl
je 0x4188
leaq 0x31eb(%rip), %rsi      # 0x7034
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4016
leaq 0x31ce(%rip), %rsi      # 0x7030
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4016
leaq 0x31d0(%rip), %rsi      # 0x704b
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4016
leaq 0x31bb(%rip), %rsi      # 0x704f
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4016
leaq 0x110(%rsp), %rdi
movq %r15, %rdx
callq 0x4474
leaq 0x3244(%rip), %rsi      # 0x7101
leaq 0x110(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
jne 0x3fa8
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x5ee4
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x5ee4
leaq 0x8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
leaq 0x70(%rsp), %rcx
movq %r14, %rsi
callq 0x4946
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2250
movq (%r14), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3f71
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x3f8b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x3fa8
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x6021(%rip), %rdi      # 0x9fd0
leaq 0x313a(%rip), %rsi      # 0x70f0
movl $0xe, %edx
callq 0x2230
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x5fff(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r14
leaq 0x311f(%rip), %rsi      # 0x70ff
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq %r14, %rdi
callq 0x20e0
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4016
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%rbx)
jmp 0x4144
leaq 0x110(%rsp), %rdi
callq 0x2140
leaq 0x8(%rsp), %rdi
movq %r15, %rdx
callq 0x4474
leaq 0x120(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2230
leaq 0x18(%rsp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x4069
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x110(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
callq 0x2120
movl 0x4c(%rsp), %esi
leaq 0x8(%rsp), %rdi
movl $0x20, %edx
callq 0x3414
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2250
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x40b5
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x5f14(%rip), %rdi      # 0x9fd0
leaq 0x302d(%rip), %rsi      # 0x70f0
movl $0xe, %edx
callq 0x2230
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x5ef2(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r14
leaq 0x3012(%rip), %rsi      # 0x70ff
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
incl (%rbx)
movq 0x5e86(%rip), %rsi      # 0x9fb0
leaq 0x110(%rsp), %rdi
callq 0x2170
leaq 0x190(%rsp), %rdi
callq 0x20a0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x415f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x4176
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x100(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2f5b(%rip), %rsi      # 0x7103
leaq 0x2f97(%rip), %rdx      # 0x7146
leaq 0xf0(%rsp), %rdi
callq 0x6088
movb $0x1, %bpl
leaq 0xf0(%rsp), %rsi
movq %r14, %rdi
callq 0x5ce8
xorl %ebp, %ebp
leaq 0x5b88(%rip), %rsi      # 0x9d60
leaq 0x1ad9(%rip), %rdx      # 0x5cb8
movq %r14, %rdi
callq 0x2310
movq %rdx, %r14
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x420f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x420f
movq %rdx, %r14
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x430d
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x430d
jmp 0x4307
movq %rdx, %r14
movq %rax, %rbx
jmp 0x432e
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x426d
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x426d
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x4439
movq %r14, %rdi
callq 0x2160
jmp 0x4439
jmp 0x4415
jmp 0x42ad
movq %rdx, %r14
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x42b3
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x42b3
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x4418
movq %rbx, %rdi
callq 0x20c0
movq %rax, %rbx
movq 0x5d01(%rip), %rdi      # 0x9fd0
leaq 0x2dca(%rip), %rsi      # 0x70a0
movl $0x1c, %edx
callq 0x2230
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x5ce0(%rip), %rdi      # 0x9fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
jmp 0x437b
jmp 0x440b
movq %rdx, %r14
movq %rax, %rbx
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x432e
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $0x1, %r14d
jne 0x4439
movq %rbx, %rdi
callq 0x20c0
movq %rax, %rbx
movq 0x5c86(%rip), %rdi      # 0x9fd0
leaq 0x2d4f(%rip), %rsi      # 0x70a0
movl $0x1c, %edx
callq 0x2230
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x5c65(%rip), %rdi      # 0x9fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2240
movq %rax, %rbx
callq 0x22f0
jmp 0x4439
movq %rax, %rbx
callq 0x22f0
jmp 0x4418
movq %rax, %rdi
callq 0x5ca7
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x4439
movq 0xc0(%rsp), %rsi
jmp 0x4401
jmp 0x43e5
jmp 0x440b
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x43e8
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x43e8
movq %rax, %rbx
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4439
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x4439
movq %rax, %rbx
jmp 0x4439
movq %rax, %rbx
jmp 0x4454
movq %rax, %rbx
movq 0x5b91(%rip), %rsi      # 0x9fb0
leaq 0x110(%rsp), %rdi
callq 0x2170
leaq 0x190(%rsp), %rdi
callq 0x20a0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4454
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x446b
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2330
nop