// File: zerolfx[P]P-Machine/sub.cpp
// Function: Sub::execute(StackMachine*)
void Sub::execute(StackMachine *stack) 
{
	// check if the stack contains at least two entries
	if(stack->fSP < 1)
		throw ExecutionError("instruction sub: requires 2 stackelements to be present.");
		
	switch(fType)
	{
		case integer:
		{
			StackInteger p1;

			// check if the two uppermost stackentries are of type integer
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
				throw ExecutionError("instruction sub: SP does not point to element of type integer.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction sub: SP - 1 does not point to element of type integer.");

			break;
		}	
		case real:
		{
			StackReal p1;
			// check if the two uppermost stackentries are of type real
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
				throw ExecutionError("instruction sub: SP does not point to element of type real.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction sub: SP - 1 does not point to element of type real.");
			break;
		}
		default:
			cerr << "operation not supported for this type" << endl;
	}
	
	// actual subtraction
	stack->fStore[stack->fSP - 1]->sub(stack->fStore[stack->fSP]);
	
	// SP := SP - 1
	delete stack->fStore[stack->fSP];
	stack->fStore[stack->fSP] = 0;
	stack->fStore.pop_back();
	--stack->fSP;	
	
	// adding cost of this instruction
	stack->fTime.count("sub");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0xb4(%rax)
jge 0x234a7
movb $0x1, -0x45(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x148(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x74f0
movq -0x140(%rbp), %rdx
leaq 0x16af0(%rip), %rsi     # 0x39f12
leaq -0x30(%rbp), %rdi
callq 0x8940
jmp 0x2342d
movq -0x148(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x283d0
jmp 0x2343f
movq -0x148(%rbp), %rdi
movb $0x0, -0x45(%rbp)
leaq 0x2662f(%rip), %rsi     # 0x49a80
leaq 0x4fa8(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x23b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x23485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x7170
leaq -0x31(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x45(%rbp)
jne 0x23496
jmp 0x234a2
movq -0x148(%rbp), %rdi
callq 0x71c0
jmp 0x23b56
movq -0x138(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x14c(%rbp)
testl %eax, %eax
je 0x23738
jmp 0x234c1
movl -0x14c(%rbp), %eax
subl $0x1, %eax
jne 0x239bd
jmp 0x234d2
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x27340
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x158(%rbp)
cmpq $0x0, %rax
jne 0x2350c
callq 0x7300
jmp 0x2350c
movq -0x158(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x275cf(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0x2352f
jmp 0x235f8
movb $0x1, -0x7a(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x168(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x74f0
movq -0x160(%rbp), %rdx
leaq 0x169e9(%rip), %rsi     # 0x39f4b
leaq -0x78(%rbp), %rdi
callq 0x8940
jmp 0x2356d
movq -0x168(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x283d0
jmp 0x2357f
movq -0x168(%rbp), %rdi
movb $0x0, -0x7a(%rbp)
leaq 0x264ef(%rip), %rsi     # 0x49a80
leaq 0x4e68(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x23b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2372a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x7170
leaq -0x79(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x7a(%rbp)
jne 0x235e7
jmp 0x235f3
movq -0x168(%rbp), %rdi
callq 0x71c0
jmp 0x2372a
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
jne 0x2362c
callq 0x7300
jmp 0x2362c
movq -0x170(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x274af(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0x2364f
jmp 0x2371c
movb $0x1, -0xa2(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x180(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x74f0
movq -0x178(%rbp), %rdx
leaq 0x16902(%rip), %rsi     # 0x39f8a
leaq -0xa0(%rbp), %rdi
callq 0x8940
jmp 0x23696
movq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x283d0
jmp 0x236ab
movq -0x180(%rbp), %rdi
movb $0x0, -0xa2(%rbp)
leaq 0x263c0(%rip), %rsi     # 0x49a80
leaq 0x4d39(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x23b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x236f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x7170
leaq -0xa1(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xa2(%rbp)
jne 0x2370e
jmp 0x2371a
movq -0x180(%rbp), %rdi
callq 0x71c0
jmp 0x2372a
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0x239df
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0x23b56
leaq -0xb8(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0x27840
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x188(%rbp)
cmpq $0x0, %rax
jne 0x23776
callq 0x7300
jmp 0x23776
movq -0x188(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x27435(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0x23799
jmp 0x2387a
movb $0x1, -0xda(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x198(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x74f0
movq -0x190(%rbp), %rdx
leaq 0x167fb(%rip), %rsi     # 0x39fcd
leaq -0xd8(%rbp), %rdi
callq 0x8940
jmp 0x237e0
movq -0x198(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x283d0
jmp 0x237f5
movq -0x198(%rbp), %rdi
movb $0x0, -0xda(%rbp)
leaq 0x26276(%rip), %rsi     # 0x49a80
leaq 0x4bef(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x23b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x239ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x23852
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x7170
leaq -0xd9(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xda(%rbp)
jne 0x23869
jmp 0x23875
movq -0x198(%rbp), %rdi
callq 0x71c0
jmp 0x239ac
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1a0(%rbp)
cmpq $0x0, %rax
jne 0x238ae
callq 0x7300
jmp 0x238ae
movq -0x1a0(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x272fd(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0x238d1
jmp 0x2399e
movb $0x1, -0x102(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1b0(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x74f0
movq -0x1a8(%rbp), %rdx
leaq 0x166ff(%rip), %rsi     # 0x3a009
leaq -0x100(%rbp), %rdi
callq 0x8940
jmp 0x23918
movq -0x1b0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x283d0
jmp 0x2392d
movq -0x1b0(%rbp), %rdi
movb $0x0, -0x102(%rbp)
leaq 0x2613e(%rip), %rsi     # 0x49a80
leaq 0x4ab7(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x23b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x23979
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x7170
leaq -0x101(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x102(%rbp)
jne 0x23990
jmp 0x2399c
movq -0x1b0(%rbp), %rdi
callq 0x71c0
jmp 0x239ac
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0x239df
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0x23b56
movq 0x2762c(%rip), %rdi     # 0x4aff0
leaq 0x13925(%rip), %rsi     # 0x372f0
callq 0x7250
movq %rax, %rdi
movq 0x275ce(%rip), %rsi     # 0x4afa8
callq 0x72b0
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1c0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq -0x1c0(%rbp), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0x23a73
movq -0x1b8(%rbp), %rdi
callq 0x26ca0
movq -0x1b8(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq $0x0, (%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x8e90
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %ecx
decl %ecx
movl %ecx, 0xb4(%rax)
movq -0x10(%rbp), %rax
addq $0x78, %rax
movq %rax, -0x1d0(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x74f0
movq -0x1c8(%rbp), %rdx
leaq 0x16569(%rip), %rsi     # 0x3a049
leaq -0x128(%rbp), %rdi
callq 0x8940
jmp 0x23aee
movq -0x1d0(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x2c4d0
jmp 0x23b03
leaq -0x128(%rbp), %rdi
callq 0x7170
leaq -0x129(%rbp), %rdi
callq 0x72e0
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x23b4a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x7170
leaq -0x129(%rbp), %rdi
callq 0x72e0
movq -0x40(%rbp), %rdi
callq 0x74e0
nop