// File: akshayrajput[P]ARMV8_Simulator/simulo.cpp
// Function: printRegMem(char*)
void printRegMem(char* arg)
{
	//TODO - parse args
	while(*arg == ' ')arg++;
	
	char c = *arg;
	arg++ ;
	//TODO - if x or d then print register
	while(*arg == ' ') arg++ ;
		
	if(c == 'x' || c == 'd')
	{
		if(*arg != 'r' && *arg != 'R')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}	
		assert(*arg == 'R' || *arg == 'r');
		arg++;
		while(*arg == ' ') arg++ ;

		int regno = (int)strtol(arg,NULL,10);
		assert(regno >= 0 && regno <= 31);

		if(c == 'x')
			printf("0x%02lx\n",R[regno].getData());
		else if(c == 'd')
			printf("%ld\n",(int64_t)R[regno].getData());
	}
	//TODO- else print memory
	else
	{
		assert(isdigit(c));
		int n = c - '0';
		while(isdigit(*arg))
		 {
			 n = n*10 + (*arg - '0') ;
			 arg++;
		 }

		while(*arg == ' ')arg++;
		
		if(*arg != 'w' && *arg != 'b' && *arg!= 'd')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}
		
		assert(*arg == 'w' || *arg == 'b' || *arg == 'd' );
		
		int offset;
		if(*arg == 'w')
			offset = 4;
		else if(*arg == 'd')
			offset = 8;
		else if(*arg == 'b')
			offset = 1;
		
		arg++;
		while(*arg == ' ') arg++;

		if(*arg != 'x' && *arg != 'd')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}
		assert(*arg == 'x' || *arg == 'd');
		c = *arg;

		arg++;

		while(*arg == ' ')arg++;

		uint64_t addr = strtoul(arg,NULL,16);
		printMem(addr,n * offset,c);
		
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x8fc6
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x8fac
movq -0x8(%rbp), %rax
movb (%rax), %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x8ff5
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x8fdb
movsbl -0x9(%rbp), %eax
cmpl $0x78, %eax
je 0x900b
movsbl -0x9(%rbp), %eax
cmpl $0x64, %eax
jne 0x9174
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
je 0x9044
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x52, %eax
je 0x9044
leaq 0x2b33(%rip), %rdi      # 0xbb5d
movb $0x0, %al
callq 0x1030
leaq 0x2b43(%rip), %rdi      # 0xbb7b
movb $0x0, %al
callq 0x1030
jmp 0x93e0
movq -0x8(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x52, %ecx
movb %al, -0x21(%rbp)
je 0x9065
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
sete %al
movb %al, -0x21(%rbp)
movb -0x21(%rbp), %al
testb $0x1, %al
jne 0x906e
jmp 0x9070
jmp 0x908f
leaq 0x2b20(%rip), %rdi      # 0xbb97
leaq 0x2aa9(%rip), %rsi      # 0xbb27
movl $0x4e, %edx
leaq 0x2b28(%rip), %rcx      # 0xbbb2
callq 0x1060
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x90b5
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x909b
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x1100
movl %eax, -0x10(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x10(%rbp)
movb %al, -0x22(%rbp)
jl 0x90df
cmpl $0x1f, -0x10(%rbp)
setle %al
movb %al, -0x22(%rbp)
movb -0x22(%rbp), %al
testb $0x1, %al
jne 0x90e8
jmp 0x90ea
jmp 0x9109
leaq 0x2ada(%rip), %rdi      # 0xbbcb
leaq 0x2a2f(%rip), %rsi      # 0xbb27
movl $0x53, %edx
leaq 0x2aae(%rip), %rcx      # 0xbbb2
callq 0x1060
movsbl -0x9(%rbp), %eax
cmpl $0x78, %eax
jne 0x913c
movslq -0x10(%rbp), %rax
leaq 0x4fc3(%rip), %rdi      # 0xe0e0
shlq $0x3, %rax
addq %rax, %rdi
callq 0xa6c0
movq %rax, %rsi
leaq 0x2ab2(%rip), %rdi      # 0xbbe5
movb $0x0, %al
callq 0x1030
jmp 0x916f
movsbl -0x9(%rbp), %eax
cmpl $0x64, %eax
jne 0x916d
movslq -0x10(%rbp), %rax
leaq 0x4f90(%rip), %rdi      # 0xe0e0
shlq $0x3, %rax
addq %rax, %rdi
callq 0xa6c0
movq %rax, %rsi
leaq 0x2a88(%rip), %rdi      # 0xbbee
movb $0x0, %al
callq 0x1030
jmp 0x916f
jmp 0x93e0
movsbl -0x9(%rbp), %edi
callq 0x1080
cmpl $0x0, %eax
je 0x9184
jmp 0x91a3
leaq 0x2a68(%rip), %rdi      # 0xbbf3
leaq 0x2995(%rip), %rsi      # 0xbb27
movl $0x5d, %edx
leaq 0x2a14(%rip), %rcx      # 0xbbb2
callq 0x1060
movsbl -0x9(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %edi
callq 0x1080
cmpl $0x0, %eax
je 0x91df
imull $0xa, -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
movsbl (%rcx), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x91ad
jmp 0x91e1
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x91fb
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x91e1
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x77, %eax
je 0x9240
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x62, %eax
je 0x9240
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
je 0x9240
leaq 0x2937(%rip), %rdi      # 0xbb5d
movb $0x0, %al
callq 0x1030
leaq 0x2947(%rip), %rdi      # 0xbb7b
movb $0x0, %al
callq 0x1030
jmp 0x93e0
movq -0x8(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x77, %ecx
movb %al, -0x23(%rbp)
je 0x9272
movq -0x8(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x62, %ecx
movb %al, -0x23(%rbp)
je 0x9272
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
sete %al
movb %al, -0x23(%rbp)
movb -0x23(%rbp), %al
testb $0x1, %al
jne 0x927b
jmp 0x927d
jmp 0x929c
leaq 0x297a(%rip), %rdi      # 0xbbfe
leaq 0x289c(%rip), %rsi      # 0xbb27
movl $0x6e, %edx
leaq 0x291b(%rip), %rcx      # 0xbbb2
callq 0x1060
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x77, %eax
jne 0x92b1
movl $0x4, -0x18(%rbp)
jmp 0x92dd
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
jne 0x92c6
movl $0x8, -0x18(%rbp)
jmp 0x92db
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x62, %eax
jne 0x92d9
movl $0x1, -0x18(%rbp)
jmp 0x92db
jmp 0x92dd
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x9303
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x92e9
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x78, %eax
je 0x933c
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
je 0x933c
leaq 0x283b(%rip), %rdi      # 0xbb5d
movb $0x0, %al
callq 0x1030
leaq 0x284b(%rip), %rdi      # 0xbb7b
movb $0x0, %al
callq 0x1030
jmp 0x93e0
movq -0x8(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x78, %ecx
movb %al, -0x24(%rbp)
je 0x935d
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
sete %al
movb %al, -0x24(%rbp)
movb -0x24(%rbp), %al
testb $0x1, %al
jne 0x9366
jmp 0x9368
jmp 0x9387
leaq 0x28b9(%rip), %rdi      # 0xbc28
leaq 0x27b1(%rip), %rsi      # 0xbb27
movl $0x81, %edx
leaq 0x2830(%rip), %rcx      # 0xbbb2
callq 0x1060
movq -0x8(%rbp), %rax
movb (%rax), %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x93b6
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x939c
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x1050
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %esi
imull -0x18(%rbp), %esi
movsbl -0x9(%rbp), %edx
callq 0x8ea0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)