// File: /workspace/github_cpp_makefile_star2_O1/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 0x4474
leaq 0x2594(%rip), %rsi      # 0x7100
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x514f
leaq 0x25ce(%rip), %rsi      # 0x7153
leaq 0x20(%rsp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x4c6e
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 0x5ee4
movl (%r15), %edx
leaq 0x150(%rsp), %rsi
movq %rbx, %rdi
callq 0x47ac
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x4c17
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4474
leaq 0x20(%rsp), %rdi
movq %rsp, %r13
movq %r13, %rsi
callq 0x2250
movq (%r13), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4c4d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x180(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c6e
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x2477(%rip), %rsi      # 0x70ec
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4ea0
leaq 0x140(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x130(%rsp), %rdi
callq 0x5ee4
leaq 0x130(%rsp), %rsi
movq %rbx, %rdi
callq 0x2b12
movl %eax, %ebp
movq 0x130(%rsp), %rdi
cmpq %r13, %rdi
je 0x4cdc
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x21f0
testb %bpl, %bpl
je 0x514f
leaq 0x2348(%rip), %rsi      # 0x7034
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4fe7
leaq 0x232b(%rip), %rsi      # 0x7030
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4fe7
leaq 0x232d(%rip), %rsi      # 0x704b
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4fe7
leaq 0x2318(%rip), %rsi      # 0x704f
leaq 0x20(%rsp), %rdi
callq 0x2080
testl %eax, %eax
je 0x4fe7
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4474
leaq 0x23a6(%rip), %rsi      # 0x7101
movq %rsp, %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x5193
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 0x4dcb
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 0x5ee4
movl 0x44(%rsp), %ecx
leaq 0x70(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movq %rbx, %rsi
callq 0x5492
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
cmpq %r13, %rdi
je 0x4e5d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xd0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4e7a
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x512f(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
jmp 0x526e
leaq 0x170(%rsp), %rdi
callq 0x2140
movq %rsp, %rdi
movq %r12, %rdx
callq 0x4474
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 0x4ee9
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 0x3414
leaq 0x50(%rsp), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
cmpq %r13, %rdi
je 0x4f32
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%r15)
leaq 0x20(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x63e8
movq %rax, %r15
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x2040
incq 0x30(%rbx)
movq 0x5072(%rip), %rdi      # 0x9fd0
leaq 0x218b(%rip), %rsi      # 0x70f0
movl $0xe, %edx
callq 0x2230
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq 0x5052(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %rbx
leaq 0x2172(%rip), %rsi      # 0x70ff
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 0x4fe8(%rip), %rsi      # 0x9fb0
leaq 0x170(%rsp), %rdi
callq 0x2170
leaq 0x1f0(%rsp), %rdi
callq 0x20a0
jmp 0x514f
leaq 0xc0(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5ee4
leaq 0xa0(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x20e0(%rip), %rdx      # 0x7100
leaq 0x90(%rsp), %rdi
movq %rdx, %rsi
callq 0x6088
leaq 0x170(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rbx, %rsi
callq 0x4946
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x5086
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x90(%rsp), %rdi
cmpq %rbp, %rdi
je 0x50a3
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x50c0
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x21f0
incl (%r15)
leaq 0x20(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x63e8
movq %rax, %r15
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x2040
incq 0x30(%rbx)
movq 0x4ee4(%rip), %rdi      # 0x9fd0
leaq 0x1ffd(%rip), %rsi      # 0x70f0
movl $0xe, %edx
callq 0x2230
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq 0x4ec4(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %rbx
leaq 0x1fe4(%rip), %rsi      # 0x70ff
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 0x516a
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x5181
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 0x5ee4
leaq 0x100(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x5ee4
leaq 0x170(%rsp), %rdi
leaq 0x110(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
movq %rbx, %rsi
callq 0x4946
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2250
movq (%r12), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x5234
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5251
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x110(%rsp), %rdi
cmpq %r13, %rdi
je 0x526e
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x50c0
movq 0x10(%rsp), %rsi
jmp 0x50b8
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x52af
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x52af
movq %rax, %rbx
movq 0x110(%rsp), %rdi
cmpq %r13, %rdi
je 0x5324
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5324
jmp 0x52f6
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5303
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5303
jmp 0x5300
jmp 0x5300
movq %rax, %rbx
jmp 0x5324
jmp 0x542e
movq %rax, %rbx
movq 0x4ca6(%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 0x5457
movq 0x10(%rsp), %rsi
jmp 0x541d
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5365
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5365
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x5457
movq 0xc0(%rsp), %rsi
jmp 0x541d
jmp 0x542e
jmp 0x542e
jmp 0x5433
jmp 0x5433
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5436
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5436
jmp 0x5433
jmp 0x542e
movq %rax, %rbx
movq 0x130(%rsp), %rdi
cmpq %r13, %rdi
je 0x5457
movq 0x140(%rsp), %rsi
jmp 0x541d
jmp 0x5401
movq %rax, %rbx
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x5404
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5404
movq %rax, %rbx
leaq 0x180(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5457
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x5457
jmp 0x542e
movq %rax, %rbx
jmp 0x5472
movq %rax, %rbx
jmp 0x5457
movq %rax, %rbx
movq 0x4b73(%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 0x5472
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x5489
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2330
nop