// File: sangwoojun[P]rv32emulator/emulator.cpp
// Function: parse_assembler_directive(int, char*, unsigned char*, int)
int parse_assembler_directive(int line, char* ftok, uint8_t* mem, int memoff) {
	//printf( "assembler directive %s\n", ftok );
	if ( 0 == memcmp(ftok, ".text", strlen(ftok) ) ) {
		if (strtok(NULL, " \t\r\n") ) {
			print_syntax_error(line, "Tokens after assembler directive");
		}
		//cur_section = SECTION_TEXT;
		memoff = TEXT_OFFSET;
		//printf( "starting text section\n" );
	} else if ( 0 == memcmp(ftok, ".data", strlen(ftok) ) ) {
		//cur_section = SECTION_TEXT;
		memoff = DATA_OFFSET;
		//printf( "starting data section\n" );
	} else if ( 0 == memcmp(ftok, ".byte", strlen(ftok)) ) memoff = parse_data_element(line, 1, mem, memoff);
	else if ( 0 == memcmp(ftok, ".half", strlen(ftok)) ) memoff = parse_data_element(line, 2, mem, memoff);
	else if ( 0 == memcmp(ftok, ".word", strlen(ftok)) ) memoff = parse_data_element(line, 4, mem, memoff);
	else if ( 0 == memcmp(ftok, ".zero", strlen(ftok)) ) memoff = parse_data_zero(line, mem, memoff);
	else {
		printf( "Undefined assembler directive at line %d: %s\n", line, ftok );
		//exit(3);
	}
	return memoff;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x28(%rbp), %rdi
movq %rax, %rdx
leaq 0x768b(%rip), %rsi      # 0xb580
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3f33
xorl %eax, %eax
movl %eax, %edi
leaq 0x7649(%rip), %rsi      # 0xb556
callq 0x2390
cmpq $0x0, %rax
je 0x3f27
movl -0x4(%rbp), %edi
leaq 0x7664(%rip), %rsi      # 0xb586
callq 0x2c90
movl $0x0, -0x1c(%rbp)
jmp 0x409f
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x30(%rbp), %rdi
movq %rax, %rdx
leaq 0x7655(%rip), %rsi      # 0xb5a7
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3f6b
movl $0x8000, -0x1c(%rbp)    # imm = 0x8000
jmp 0x409d
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x38(%rbp), %rdi
movq %rax, %rdx
leaq 0x7623(%rip), %rsi      # 0xb5ad
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3fb3
movl -0x4(%rbp), %edi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl $0x1, %esi
callq 0x3d90
movl %eax, -0x1c(%rbp)
jmp 0x409b
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x40(%rbp), %rdi
movq %rax, %rdx
leaq 0x75e1(%rip), %rsi      # 0xb5b3
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3ffb
movl -0x4(%rbp), %edi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl $0x2, %esi
callq 0x3d90
movl %eax, -0x1c(%rbp)
jmp 0x4099
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0x759f(%rip), %rsi      # 0xb5b9
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x4040
movl -0x4(%rbp), %edi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl $0x4, %esi
callq 0x3d90
movl %eax, -0x1c(%rbp)
jmp 0x4097
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2100
movq -0x50(%rbp), %rdi
movq %rax, %rdx
leaq 0x7560(%rip), %rsi      # 0xb5bf
callq 0x2110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x4080
movl -0x4(%rbp), %edi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x3e60
movl %eax, -0x1c(%rbp)
jmp 0x4095
movl -0x4(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq 0x7537(%rip), %rdi      # 0xb5c5
movb $0x0, %al
callq 0x2040
jmp 0x4097
jmp 0x4099
jmp 0x409b
jmp 0x409d
jmp 0x409f
movl -0x1c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)