// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x290, %rsp            # imm = 0x290
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x8(%rsp), %rdi
movq %rsi, %rdx
callq 0x38d6
leaq 0x2d3f(%rip), %rsi      # 0x60e4
leaq 0x8(%rsp), %rdi
callq 0x4949
testb %al, %al
jne 0x36ad
leaq 0x2d79(%rip), %rsi      # 0x6137
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x2360
cmpq $-0x1, %rax
je 0x3457
leaq 0x8(%rsp), %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x108(%rsp), %rdi
xorl %edx, %edx
callq 0x2210
leaq 0xc8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x2250
movl (%rbx), %edx
leaq 0xc8(%rsp), %rsi
movq %r14, %rdi
callq 0x3b48
leaq 0xc8(%rsp), %rdi
callq 0x20e0
leaq 0x28(%rsp), %rdi
movq %r15, %rdx
callq 0x38d6
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %r12
movq %r12, %rsi
callq 0x2230
movq %r12, %rdi
callq 0x20e0
leaq 0x108(%rsp), %rdi
callq 0x20e0
leaq 0x2c72(%rip), %rsi      # 0x60d0
leaq 0x8(%rsp), %rdi
callq 0x4949
testb %al, %al
je 0x352e
leaq 0x108(%rsp), %rdi
callq 0x2160
leaq 0x28(%rsp), %rdi
movq %r15, %rdx
callq 0x38d6
leaq 0x118(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x21d0
leaq 0x28(%rsp), %rdi
callq 0x20e0
leaq 0x108(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x2150
movl 0x4(%rsp), %esi
leaq 0x28(%rsp), %rdi
pushq $0x20
popq %rdx
callq 0x2dd6
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x2230
movq %r14, %rdi
callq 0x20e0
movq 0x6ae6(%rip), %rdi      # 0x9fd0
leaq 0x2be3(%rip), %rsi      # 0x60d4
callq 0x21e0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x21d0
leaq 0x2bd9(%rip), %rsi      # 0x60e3
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
incl (%rbx)
leaq 0x108(%rsp), %rdi
callq 0x2190
jmp 0x36ad
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2250
leaq 0xa8(%rsp), %rsi
movq %r14, %rdi
callq 0x289c
movl %eax, %ebp
leaq 0xa8(%rsp), %rdi
callq 0x20e0
testb %bpl, %bpl
je 0x36d1
leaq 0x2aa9(%rip), %rsi      # 0x6018
leaq 0x8(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x36ab
leaq 0x2a8c(%rip), %rsi      # 0x6014
leaq 0x8(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x36ab
leaq 0x2a8e(%rip), %rsi      # 0x602f
leaq 0x8(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x36ab
leaq 0x2a79(%rip), %rsi      # 0x6033
leaq 0x8(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x36ab
leaq 0x108(%rsp), %rdi
movq %r15, %rdx
callq 0x38d6
leaq 0x2b02(%rip), %rsi      # 0x60e5
leaq 0x108(%rsp), %rdi
xorl %edx, %edx
callq 0x2360
cmpq $-0x1, %rax
jne 0x3667
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2250
leaq 0x68(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x2250
leaq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq %r14, %rsi
callq 0x3c40
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x2230
movq %r14, %rdi
callq 0x20e0
leaq 0x68(%rsp), %rdi
callq 0x20e0
leaq 0x88(%rsp), %rdi
callq 0x20e0
movq 0x6962(%rip), %rdi      # 0x9fd0
leaq 0x2a5f(%rip), %rsi      # 0x60d4
callq 0x21e0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x21d0
leaq 0x2a55(%rip), %rsi      # 0x60e3
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
leaq 0x108(%rsp), %rdi
callq 0x20e0
incl (%rbx)
leaq 0x8(%rsp), %rdi
callq 0x20e0
leaq 0x48(%rsp), %rdi
callq 0x20e0
addq $0x290, %rsp            # imm = 0x290
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x2100
movq %rax, %r14
leaq 0x2a04(%rip), %rsi      # 0x60e7
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x4630
movb $0x1, %bpl
leaq 0xe8(%rsp), %rsi
movq %r14, %rdi
callq 0x4914
xorl %ebp, %ebp
leaq 0x665c(%rip), %rsi      # 0x9d70
leaq 0x11d9(%rip), %rdx      # 0x48f4
movq %r14, %rdi
callq 0x22e0
movq %rdx, %r14
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x20e0
jmp 0x373b
movq %rdx, %r14
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x20e0
jmp 0x375a
jmp 0x3754
movq %rdx, %r14
movq %rax, %rbx
jmp 0x3767
movq %rdx, %r14
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x20e0
cmpl $0x1, %r14d
jne 0x38b5
movq %rbx, %rdi
callq 0x20b0
movq %rax, %rbx
movq 0x684d(%rip), %rdi      # 0x9fd0
leaq 0x28fa(%rip), %rsi      # 0x6084
callq 0x21e0
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
jmp 0x385d
movq %rax, %rbx
callq 0x22d0
jmp 0x38b5
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x20e0
testb %bpl, %bpl
jne 0x37dd
jmp 0x38b5
movq %rax, %rbx
movq %r14, %rdi
callq 0x2180
jmp 0x38b5
jmp 0x38a5
jmp 0x3810
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20e0
jmp 0x3816
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x3887
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x38a8
movq %rbx, %rdi
callq 0x20b0
movq %rax, %rbx
movq 0x679e(%rip), %rdi      # 0x9fd0
leaq 0x284b(%rip), %rsi      # 0x6084
callq 0x21e0
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
xorl %edi, %edi
callq 0x2220
jmp 0x388e
jmp 0x388e
jmp 0x387c
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x20e0
jmp 0x387f
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x20e0
jmp 0x38b5
movq %rax, %rbx
jmp 0x38b5
movq %rax, %rbx
callq 0x22d0
jmp 0x38a8
movq %rax, %rdi
callq 0x48e3
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x2190
leaq 0x8(%rsp), %rdi
callq 0x20e0
leaq 0x48(%rsp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x22f0
movq %rax, %rbx
jmp 0x38bf