// File: /workspace/github_cpp_makefile_star2_O3/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 0x4442
leaq 0x344e(%rip), %rsi      # 0x70e4
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4116
leaq 0x3488(%rip), %rsi      # 0x7137
leaq 0x28(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x3d9c
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 0x5ebe
movl (%rbx), %edx
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x475c
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x3d41
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x8(%rsp), %rdi
movq %r15, %rdx
callq 0x4442
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3d7b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d9c
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x332d(%rip), %rsi      # 0x70d0
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x3fef
leaq 0xc0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5ebe
leaq 0xb0(%rsp), %rsi
movq %r14, %rdi
callq 0x2ada
movl %eax, %ebp
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x3e0b
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x21f0
testb %bpl, %bpl
je 0x415a
leaq 0x31fd(%rip), %rsi      # 0x7018
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x3fe8
leaq 0x31e0(%rip), %rsi      # 0x7014
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x3fe8
leaq 0x31e2(%rip), %rsi      # 0x702f
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x3fe8
leaq 0x31cd(%rip), %rsi      # 0x7033
leaq 0x28(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x3fe8
leaq 0x110(%rsp), %rdi
movq %r15, %rdx
callq 0x4442
leaq 0x3256(%rip), %rsi      # 0x70e5
leaq 0x110(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
jne 0x3f7a
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x5ebe
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x5ebe
leaq 0x8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
leaq 0x70(%rsp), %rcx
movq %r14, %rsi
callq 0x48f6
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2250
movq (%r14), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3f43
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x3f5d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x3f7a
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x604f(%rip), %rdi      # 0x9fd0
leaq 0x314c(%rip), %rsi      # 0x70d4
movl $0xe, %edx
callq 0x2230
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x602d(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r14
leaq 0x3131(%rip), %rsi      # 0x70e3
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 0x3fe8
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%rbx)
jmp 0x4116
leaq 0x110(%rsp), %rdi
callq 0x2140
leaq 0x8(%rsp), %rdi
movq %r15, %rdx
callq 0x4442
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 0x403b
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 0x33fa
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2250
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x4087
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x5f42(%rip), %rdi      # 0x9fd0
leaq 0x303f(%rip), %rsi      # 0x70d4
movl $0xe, %edx
callq 0x2230
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x5f20(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r14
leaq 0x3024(%rip), %rsi      # 0x70e3
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 0x5eb4(%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 0x4131
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x4148
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 0x2f6d(%rip), %rsi      # 0x70e7
leaq 0x2fa9(%rip), %rdx      # 0x712a
leaq 0xf0(%rsp), %rdi
callq 0x605e
movb $0x1, %bpl
leaq 0xf0(%rsp), %rsi
movq %r14, %rdi
callq 0x5cc2
xorl %ebp, %ebp
leaq 0x5bb6(%rip), %rsi      # 0x9d60
leaq 0x1ae1(%rip), %rdx      # 0x5c92
movq %r14, %rdi
callq 0x2310
movq %rdx, %r14
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x41e1
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x41e1
movq %rdx, %r14
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x42db
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x42db
jmp 0x42d5
movq %rdx, %r14
movq %rax, %rbx
jmp 0x42fc
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x4237
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
testb %bpl, %bpl
jne 0x4244
jmp 0x4407
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0x4407
jmp 0x43e3
jmp 0x427b
movq %rdx, %r14
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4281
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x4281
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x43e6
movq %rbx, %rdi
callq 0x20c0
movq %rax, %rbx
movq 0x5d33(%rip), %rdi      # 0x9fd0
leaq 0x2de0(%rip), %rsi      # 0x7084
movl $0x1c, %edx
callq 0x2230
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x5d12(%rip), %rdi      # 0x9fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
jmp 0x4349
jmp 0x43d9
movq %rdx, %r14
movq %rax, %rbx
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x42fc
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $0x1, %r14d
jne 0x4407
movq %rbx, %rdi
callq 0x20c0
movq %rax, %rbx
movq 0x5cb8(%rip), %rdi      # 0x9fd0
leaq 0x2d65(%rip), %rsi      # 0x7084
movl $0x1c, %edx
callq 0x2230
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x5c97(%rip), %rdi      # 0x9fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2240
movq %rax, %rbx
callq 0x22f0
jmp 0x4407
movq %rax, %rbx
callq 0x22f0
jmp 0x43e6
movq %rax, %rdi
callq 0x5c81
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x4407
movq 0xc0(%rsp), %rsi
jmp 0x43cf
jmp 0x43b3
jmp 0x43d9
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x43b6
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x43b6
movq %rax, %rbx
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4407
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x4407
movq %rax, %rbx
jmp 0x4407
movq %rax, %rbx
jmp 0x4422
movq %rax, %rbx
movq 0x5bc3(%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 0x4422
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x4439
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2330
nop