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

	string parsed = parse(line);

	if (parsed == "")
	{
		return;
	}

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

		addVariableToSymbolTable(label, *lineCounter);

		parsed = parse(line);

	}

	// if "VAR", declareVar(next word) and push to buffer
	if (parsed == "VAR")
	{
		int var;
		stringstream ss;
		ss << parse(line);
		ss >> var;
		

		bufferLine = declareVariable(var);

		(*lineCounter)++;
		it = buffer.insert(it, bufferLine);
		cout << "Machine Code: " << *it << "\n" << endl;
	}
	else if(iSet.contains(parsed)) // recognised instruction
	{
		if (parsed != "STP" && parsed != "CMP" && parsed != "INC" && parsed != "DEC") // requires operand
		{
			string instrOperand = parse(line);
			if (instrOperand.find("#") == string::npos) // absolute addressing (mem address)
			{
				bufferLine = encodeInstructionAddress(parsed, instrOperand);
			}
			else //direct addressing (#value)
			{
				int value;
				stringstream ss;
				// remove #
				instrOperand = instrOperand.substr(1, instrOperand.length() - 1); 
				//convert to int
				ss << instrOperand;
				ss >> value;
				// encode
				bufferLine = encodeInstructionValue(parsed, value);
			}
		}
		else // blank operand
		{
			bufferLine = encodeInstructionAddress(parsed, "");
		}

		(*lineCounter) = (*lineCounter + 1);

		it = buffer.insert(it, bufferLine);
		cout << "Machine Code: " << *it << "\n" << endl;

 	}
 	else
 	{

 	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rcx, 0x48(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x60(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x20(%rsp), %rdi
movq %rsi, %rdx
callq 0x4442
leaq 0x25c8(%rip), %rsi      # 0x70e4
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x50ff
leaq 0x2602(%rip), %rsi      # 0x7137
leaq 0x20(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x4c1e
leaq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x170(%rsp), %rdi
xorl %edx, %edx
callq 0x2210
leaq 0x160(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x170(%rsp), %rsi
movq 0x178(%rsp), %rdx
addq %rsi, %rdx
leaq 0x150(%rsp), %rdi
callq 0x5ebe
movl (%r15), %edx
leaq 0x150(%rsp), %rsi
movq %rbx, %rdi
callq 0x475c
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x4bc7
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4442
leaq 0x20(%rsp), %rdi
movq %rsp, %r13
movq %r13, %rsi
callq 0x2250
movq (%r13), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4bfd
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x180(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c1e
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x24ab(%rip), %rsi      # 0x70d0
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4e50
leaq 0x140(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x130(%rsp), %rdi
callq 0x5ebe
leaq 0x130(%rsp), %rsi
movq %rbx, %rdi
callq 0x2ada
movl %eax, %ebp
movq 0x130(%rsp), %rdi
cmpq %r13, %rdi
je 0x4c8c
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x21f0
testb %bpl, %bpl
je 0x50ff
leaq 0x237c(%rip), %rsi      # 0x7018
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4f97
leaq 0x235f(%rip), %rsi      # 0x7014
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4f97
leaq 0x2361(%rip), %rsi      # 0x702f
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4f97
leaq 0x234c(%rip), %rsi      # 0x7033
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4f97
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4442
leaq 0x23da(%rip), %rsi      # 0x70e5
movq %rsp, %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x5143
leaq 0x170(%rsp), %rdi
callq 0x2140
movq %rsp, %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x70(%rsp), %rdi
movl $0x1, %edx
callq 0x2210
movq %rsp, %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x80(%rsp), %r13
cmpq %r13, %rdi
je 0x4d7b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x180(%rsp), %rdi
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x2230
leaq 0x170(%rsp), %rdi
leaq 0x44(%rsp), %rsi
callq 0x2120
leaq 0xe0(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x5ebe
movl 0x44(%rsp), %ecx
leaq 0x70(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movq %rbx, %rsi
callq 0x5442
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
cmpq %r13, %rdi
je 0x4e0d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xd0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4e2a
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x517f(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
jmp 0x521e
leaq 0x170(%rsp), %rdi
callq 0x2140
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4442
leaq 0x180(%rsp), %rdi
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x2230
leaq 0x10(%rsp), %r13
movq -0x10(%r13), %rdi
cmpq %r13, %rdi
je 0x4e99
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x170(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2120
movl 0x70(%rsp), %esi
movq %rsp, %rdi
movl $0x20, %edx
callq 0x33fa
leaq 0x50(%rsp), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
cmpq %r13, %rdi
je 0x4ee2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%r15)
leaq 0x20(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x63c4
movq %rax, %r15
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x2040
incq 0x30(%rbx)
movq 0x50c2(%rip), %rdi      # 0x9fd0
leaq 0x21bf(%rip), %rsi      # 0x70d4
movl $0xe, %edx
callq 0x2230
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq 0x50a2(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %rbx
leaq 0x21a6(%rip), %rsi      # 0x70e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x5038(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
jmp 0x50ff
leaq 0xc0(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5ebe
leaq 0xa0(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x2114(%rip), %rdx      # 0x70e4
leaq 0x90(%rsp), %rdi
movq %rdx, %rsi
callq 0x605e
leaq 0x170(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rbx, %rsi
callq 0x48f6
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x5036
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x90(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5053
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x5070
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%r15)
leaq 0x20(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x63c4
movq %rax, %r15
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x2040
incq 0x30(%rbx)
movq 0x4f34(%rip), %rdi      # 0x9fd0
leaq 0x2031(%rip), %rsi      # 0x70d4
movl $0xe, %edx
callq 0x2230
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq 0x4f14(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %rbx
leaq 0x2018(%rip), %rsi      # 0x70e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x511a
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x5131
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x120(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x110(%rsp), %rdi
callq 0x5ebe
leaq 0x100(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x5ebe
leaq 0x170(%rsp), %rdi
leaq 0x110(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
movq %rbx, %rsi
callq 0x48f6
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x51e4
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5201
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x110(%rsp), %rdi
cmpq %r13, %rdi
je 0x521e
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5070
movq 0x10(%rsp), %rsi
jmp 0x5068
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x525f
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x525f
movq %rax, %rbx
movq 0x110(%rsp), %rdi
cmpq %r13, %rdi
je 0x52d4
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x52d4
jmp 0x52a6
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x52b3
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x52b3
jmp 0x52b0
jmp 0x52b0
movq %rax, %rbx
jmp 0x52d4
jmp 0x53de
movq %rax, %rbx
movq 0x4cf6(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5407
movq 0x10(%rsp), %rsi
jmp 0x53cd
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5315
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5315
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x5407
movq 0xc0(%rsp), %rsi
jmp 0x53cd
jmp 0x53de
jmp 0x53de
jmp 0x53e3
jmp 0x53e3
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53e6
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x53e6
jmp 0x53e3
jmp 0x53de
movq %rax, %rbx
movq 0x130(%rsp), %rdi
cmpq %r13, %rdi
je 0x5407
movq 0x140(%rsp), %rsi
jmp 0x53cd
jmp 0x53b1
movq %rax, %rbx
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x53b4
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x53b4
movq %rax, %rbx
leaq 0x180(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5407
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5407
jmp 0x53de
movq %rax, %rbx
jmp 0x5422
movq %rax, %rbx
jmp 0x5407
movq %rax, %rbx
movq 0x4bc3(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5422
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x5439
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2330
nop