// File: tmtxt[P]microcontroller-emulation/main.cpp
// Function: optionModifyMemoryHandler(mcontroller::Microcontroller**)
void optionModifyMemoryHandler(mcontroller::Microcontroller** microcontroller){

	// display the message for user
	cout << "Message: Modify memory." << endl;
	cout << "Message: Enter the memory location to modify (hexadeciaml format)." << endl;
	
	// The input memory address
	int location;

	// input address from user
	location = (*microcontroller)->lookUpMemoryAddress();

	// check if the location is valid
	if(location != Microcontroller::memoryLocationInvalid){
		// print the old value
		unsigned int oldValue = (*microcontroller)->getMemoryValueAtLocation(location);
		cout << "Message: Old value: ";
		cout << hex << oldValue;
		cout << endl;

		// print the message
		cout << "Message: Enter the new value (hexadecimal format)." << endl;
		cout << "new? ";

		// read the input value from user
		unsigned int inputNewValue;
		// validate input
		while(!(cin >> hex >> inputNewValue)){
			cerr << "Error: Please input a valid hexadecimal integer!" << endl;
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "news? ";
		}

		// As each memory location is a byte size (unsigned char), only the
		// lower 8 bits of input should be stored.
		unsigned char newValue = (inputNewValue & 0xFF);
		(*microcontroller)->setMemoryValueAtLocation(location, newValue);

		// clear the cin
		cin.clear();
		cin.ignore(1000, '\n');
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq 0xb035(%rip), %rdi      # 0xefc8
leaq 0x639e(%rip), %rsi      # 0xa338
callq 0x31e0
movq %rax, %rdi
movq 0xb00f(%rip), %rsi      # 0xefb8
callq 0x3220
movq 0xb013(%rip), %rdi      # 0xefc8
leaq 0x6394(%rip), %rsi      # 0xa350
callq 0x31e0
movq %rax, %rdi
movq 0xafed(%rip), %rsi      # 0xefb8
callq 0x3220
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x4eb0
movl %eax, -0xc(%rbp)
cmpl $-0x1, -0xc(%rbp)
je 0x4167
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0xc(%rbp), %esi
callq 0x4df0
movzbl %al, %eax
movl %eax, -0x10(%rbp)
movq 0xafc3(%rip), %rdi      # 0xefc8
leaq 0x6387(%rip), %rsi      # 0xa393
callq 0x31e0
movq 0xafb0(%rip), %rdi      # 0xefc8
leaq 0x401(%rip), %rsi       # 0x4420
callq 0x3090
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x32b0
movq 0xaf92(%rip), %rdi      # 0xefc8
movq 0xaf7b(%rip), %rsi      # 0xefb8
callq 0x3220
movq 0xaf7f(%rip), %rdi      # 0xefc8
leaq 0x6358(%rip), %rsi      # 0xa3a8
callq 0x31e0
movq %rax, %rdi
movq 0xaf59(%rip), %rsi      # 0xefb8
callq 0x3220
movq 0xaf5d(%rip), %rdi      # 0xefc8
leaq 0x6369(%rip), %rsi      # 0xa3db
callq 0x31e0
movq 0xaf5a(%rip), %rdi      # 0xefd8
leaq 0x39b(%rip), %rsi       # 0x4420
callq 0x3330
movq %rax, %rdi
leaq -0x14(%rbp), %rsi
callq 0x3050
movq %rax, %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x30e0
testb $0x1, %al
jne 0x40ab
jmp 0x4117
movq 0xaf3e(%rip), %rdi      # 0xeff0
leaq 0x6328(%rip), %rsi      # 0xa3e1
callq 0x31e0
movq %rax, %rdi
movq 0xaef0(%rip), %rsi      # 0xefb8
callq 0x3220
movq 0xaf04(%rip), %rax      # 0xefd8
movq (%rax), %rax
movq 0xaefa(%rip), %rdi      # 0xefd8
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x3340
movq 0xaee8(%rip), %rdi      # 0xefd8
movl $0x3e8, %esi            # imm = 0x3E8
movl $0xa, %edx
callq 0x3120
movq 0xaec2(%rip), %rdi      # 0xefc8
leaq 0x6305(%rip), %rsi      # 0xa412
callq 0x31e0
jmp 0x4077
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x15(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0xc(%rbp), %esi
movzbl -0x15(%rbp), %edx
callq 0x4e50
movq 0xae9c(%rip), %rax      # 0xefd8
movq (%rax), %rax
movq 0xae92(%rip), %rdi      # 0xefd8
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x3340
movq 0xae80(%rip), %rdi      # 0xefd8
movl $0x3e8, %esi            # imm = 0x3E8
movl $0xa, %edx
callq 0x3120
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)