// File: 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
movq %rsp, %rbp
subq $0x670, %rsp            # imm = 0x670
movq %rcx, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x5c0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3390
movq -0x5c0(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x5ea0
jmp 0x6715
leaq 0x49c8(%rip), %rsi      # 0xb0e4
leaq -0x60(%rbp), %rdi
callq 0x80b0
movb %al, -0x5c1(%rbp)
jmp 0x672d
movb -0x5c1(%rbp), %al
testb $0x1, %al
jne 0x6739
jmp 0x6767
movl $0x1, -0x70(%rbp)
jmp 0x6f94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6fb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6faf
leaq -0x60(%rbp), %rdi
leaq 0x49c5(%rip), %rsi      # 0xb137
xorl %eax, %eax
movl %eax, %edx
callq 0x34c0
cmpq $-0x1, %rax
je 0x686a
leaq -0x60(%rbp), %rdi
movq %rdi, -0x5d0(%rbp)
callq 0x33d0
movq -0x5d0(%rbp), %rsi
movq %rax, %rcx
decq %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x90(%rbp), %rdi
callq 0x32c0
jmp 0x67b4
leaq -0xb0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3100
jmp 0x67c9
movq -0x5c0(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq -0xb0(%rbp), %rsi
callq 0x62c0
jmp 0x67e4
leaq -0xb0(%rbp), %rdi
callq 0x31c0
movq -0x5c0(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x5ea0
jmp 0x6809
leaq -0x60(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3330
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0x90(%rbp), %rdi
callq 0x31c0
jmp 0x686a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6859
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x31c0
leaq -0x90(%rbp), %rdi
callq 0x31c0
jmp 0x6faf
leaq 0x485f(%rip), %rsi      # 0xb0d0
leaq -0x60(%rbp), %rdi
callq 0x80b0
movb %al, -0x5d1(%rbp)
jmp 0x6882
movb -0x5d1(%rbp), %al
testb $0x1, %al
jne 0x6891
jmp 0x6a5f
leaq -0x260(%rbp), %rdi
callq 0x31f0
jmp 0x689f
movq -0x5c0(%rbp), %rsi
leaq -0x250(%rbp), %rax
movq %rax, -0x5e0(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x280(%rbp), %rdi
callq 0x5ea0
jmp 0x68c6
movq -0x5e0(%rbp), %rdi
leaq -0x280(%rbp), %rsi
callq 0x3270
jmp 0x68db
leaq -0x280(%rbp), %rdi
callq 0x31c0
leaq -0x260(%rbp), %rdi
leaq -0xd4(%rbp), %rsi
callq 0x31b0
jmp 0x68fc
movq -0x5c0(%rbp), %rsi
movl -0xd4(%rbp), %edx
leaq -0x2a0(%rbp), %rdi
callq 0x6420
jmp 0x6917
leaq -0x40(%rbp), %rdi
movq %rdi, -0x5f0(%rbp)
leaq -0x2a0(%rbp), %rsi
movq %rsi, -0x600(%rbp)
callq 0x3330
movq -0x600(%rbp), %rdi
callq 0x31c0
movq -0x5c0(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
addq $0x20, %rax
movq %rax, -0x5f8(%rbp)
leaq -0x2b0(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x81b0
movq -0x5f8(%rbp), %rdi
movq -0x5f0(%rbp), %rdx
movq -0x2b0(%rbp), %rsi
callq 0x8140
movq %rax, -0x5e8(%rbp)
jmp 0x6990
movq -0x5e8(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq 0x9620(%rip), %rdi      # 0xffd0
leaq 0x471d(%rip), %rsi      # 0xb0d4
callq 0x3290
movq %rax, -0x608(%rbp)
jmp 0x69c5
leaq -0x8(%rbp), %rdi
callq 0x81d0
movq -0x608(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
movq %rax, -0x610(%rbp)
jmp 0x69e6
movq -0x610(%rbp), %rdi
leaq 0x46ef(%rip), %rsi      # 0xb0e3
callq 0x3290
movq %rax, -0x618(%rbp)
jmp 0x6a02
movq -0x618(%rbp), %rdi
movq 0x95a8(%rip), %rsi      # 0xffb8
callq 0x32d0
jmp 0x6a17
leaq -0x260(%rbp), %rdi
callq 0x3220
jmp 0x6f8d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6a4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x31c0
leaq -0x260(%rbp), %rdi
callq 0x3220
jmp 0x6faf
movq -0x5c0(%rbp), %rax
movq %rax, -0x620(%rbp)
leaq -0x2d0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3100
jmp 0x6a7f
movq -0x620(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x4100
movb %al, -0x621(%rbp)
jmp 0x6a9a
leaq -0x2d0(%rbp), %rdi
callq 0x31c0
movb -0x621(%rbp), %al
testb $0x1, %al
jne 0x6ab5
jmp 0x6f89
leaq 0x455c(%rip), %rsi      # 0xb018
leaq -0x60(%rbp), %rdi
callq 0x80e0
movb %al, -0x622(%rbp)
jmp 0x6acd
movb -0x622(%rbp), %al
testb $0x1, %al
jne 0x6adc
jmp 0x6dcb
leaq 0x4531(%rip), %rsi      # 0xb014
leaq -0x60(%rbp), %rdi
callq 0x80e0
movb %al, -0x623(%rbp)
jmp 0x6af4
movb -0x623(%rbp), %al
testb $0x1, %al
jne 0x6b03
jmp 0x6dcb
leaq 0x4525(%rip), %rsi      # 0xb02f
leaq -0x60(%rbp), %rdi
callq 0x80e0
movb %al, -0x624(%rbp)
jmp 0x6b1b
movb -0x624(%rbp), %al
testb $0x1, %al
jne 0x6b2a
jmp 0x6dcb
leaq 0x4502(%rip), %rsi      # 0xb033
leaq -0x60(%rbp), %rdi
callq 0x80e0
movb %al, -0x625(%rbp)
jmp 0x6b42
movb -0x625(%rbp), %al
testb $0x1, %al
jne 0x6b51
jmp 0x6dcb
movq -0x5c0(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x2f0(%rbp), %rdi
callq 0x5ea0
jmp 0x6b6a
leaq -0x2f0(%rbp), %rdi
leaq 0x456d(%rip), %rsi      # 0xb0e5
xorl %eax, %eax
movl %eax, %edx
callq 0x34c0
cmpq $-0x1, %rax
jne 0x6c73
leaq -0x330(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3100
jmp 0x6b9d
leaq -0x350(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x3100
jmp 0x6bb2
movq -0x5c0(%rbp), %rsi
leaq -0x310(%rbp), %rdi
leaq -0x330(%rbp), %rdx
leaq -0x350(%rbp), %rcx
callq 0x6460
jmp 0x6bd5
leaq -0x40(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x3330
leaq -0x310(%rbp), %rdi
callq 0x31c0
leaq -0x350(%rbp), %rdi
callq 0x31c0
leaq -0x330(%rbp), %rdi
callq 0x31c0
jmp 0x6da9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x31c0
jmp 0x6faf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6dba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6c62
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x350(%rbp), %rdi
callq 0x31c0
leaq -0x330(%rbp), %rdi
callq 0x31c0
jmp 0x6dba
leaq -0x4e0(%rbp), %rdi
callq 0x31f0
jmp 0x6c81
leaq -0x2f0(%rbp), %rdi
movq %rdi, -0x630(%rbp)
callq 0x33d0
movq -0x630(%rbp), %rsi
movq %rax, %rcx
decq %rcx
leaq -0x500(%rbp), %rdi
movl $0x1, %edx
callq 0x32c0
jmp 0x6cb4
leaq -0x2f0(%rbp), %rdi
movq %rdi, -0x638(%rbp)
leaq -0x500(%rbp), %rsi
movq %rsi, -0x640(%rbp)
callq 0x3330
movq -0x640(%rbp), %rdi
callq 0x31c0
movq -0x638(%rbp), %rsi
leaq -0x4d0(%rbp), %rdi
callq 0x3270
jmp 0x6cf6
leaq -0x4e0(%rbp), %rdi
leaq -0x354(%rbp), %rsi
callq 0x31b0
jmp 0x6d0b
leaq -0x540(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3100
jmp 0x6d1d
movq -0x5c0(%rbp), %rsi
movl -0x354(%rbp), %ecx
leaq -0x520(%rbp), %rdi
leaq -0x540(%rbp), %rdx
callq 0x6fd0
jmp 0x6d3f
leaq -0x40(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x3330
leaq -0x520(%rbp), %rdi
callq 0x31c0
leaq -0x540(%rbp), %rdi
callq 0x31c0
leaq -0x4e0(%rbp), %rdi
callq 0x3220
jmp 0x6da9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6d9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x540(%rbp), %rdi
callq 0x31c0
leaq -0x4e0(%rbp), %rdi
callq 0x3220
jmp 0x6dba
leaq -0x2f0(%rbp), %rdi
callq 0x31c0
jmp 0x6eb4
leaq -0x2f0(%rbp), %rdi
callq 0x31c0
jmp 0x6faf
leaq -0x580(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3100
jmp 0x6ddd
leaq -0x5a1(%rbp), %rdi
movq %rdi, -0x648(%rbp)
callq 0x3460
movq -0x648(%rbp), %rdx
leaq 0x42e6(%rip), %rsi      # 0xb0e4
leaq -0x5a0(%rbp), %rdi
callq 0x7630
jmp 0x6e0c
movq -0x5c0(%rbp), %rsi
leaq -0x560(%rbp), %rdi
leaq -0x580(%rbp), %rdx
leaq -0x5a0(%rbp), %rcx
callq 0x6460
jmp 0x6e2f
leaq -0x40(%rbp), %rdi
leaq -0x560(%rbp), %rsi
callq 0x3330
leaq -0x560(%rbp), %rdi
callq 0x31c0
leaq -0x5a0(%rbp), %rdi
callq 0x31c0
leaq -0x5a1(%rbp), %rdi
callq 0x32f0
leaq -0x580(%rbp), %rdi
callq 0x31c0
jmp 0x6eb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6e97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x5a0(%rbp), %rdi
callq 0x31c0
leaq -0x5a1(%rbp), %rdi
callq 0x32f0
leaq -0x580(%rbp), %rdi
callq 0x31c0
jmp 0x6faf
movq -0x5c0(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
addq $0x20, %rax
movq %rax, -0x658(%rbp)
leaq -0x5b8(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x81b0
movq -0x658(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x8140
movq %rax, -0x650(%rbp)
jmp 0x6f00
movq -0x650(%rbp), %rax
movq %rax, -0x5b0(%rbp)
movq -0x5b0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq 0x90b0(%rip), %rdi      # 0xffd0
leaq 0x41ad(%rip), %rsi      # 0xb0d4
callq 0x3290
movq %rax, -0x660(%rbp)
jmp 0x6f35
leaq -0x8(%rbp), %rdi
callq 0x81d0
movq -0x660(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
movq %rax, -0x668(%rbp)
jmp 0x6f56
movq -0x668(%rbp), %rdi
leaq 0x417f(%rip), %rsi      # 0xb0e3
callq 0x3290
movq %rax, -0x670(%rbp)
jmp 0x6f72
movq -0x670(%rbp), %rdi
movq 0x9038(%rip), %rsi      # 0xffb8
callq 0x32d0
jmp 0x6f87
jmp 0x6f8b
jmp 0x6f8b
jmp 0x6f8d
movl $0x0, -0x70(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x31c0
leaq -0x40(%rbp), %rdi
callq 0x31c0
addq $0x670, %rsp            # imm = 0x670
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x31c0
leaq -0x40(%rbp), %rdi
callq 0x31c0
movq -0x68(%rbp), %rdi
callq 0x3450
nopw (%rax,%rax)