-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_assembler_utils.h
64 lines (58 loc) · 2.71 KB
/
my_assembler_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*****************************************************************
* BUAA Fall 2021 Fundamentals of Computer Hardware
* Project7 Assembler and Linker
*****************************************************************
* my_assembler_utils.h
* Assembler Functions Description
*****************************************************************/
#ifndef MY_UTILS_H
#define MY_UTILS_H
#include "linker-src/linker_utils.h"
/*******************************
* Linker function
*******************************/
/*
* This function reads .data symbol from INPUT and add them to the SYMTBL
* Note that in order to distinguish in the symbol table whether a symbol
* comes from the .data segment or the .text segment, we append a % to the
* symbol name when adding the .data segment symbol. Since only underscores and
* letters will appear in legal symbols, distinguishing them by adding % will
* not cause a conflict between the new symbol and the symbol in the assembly file.
*
* Return value:
* Return the number of bytes in the .data segment.
*/
int read_data_segment(FILE *input, SymbolTable *symtbl);
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE.
* ADDR is given as the byte offset from the first instruction. The SymbolTable
* must be able to resize itself as more elements are added.
*
* Note that NAME may point to a temporary array, so it is not safe to simply
* store the NAME pointer. You must store a copy of the given string.
*
* If ADDR is not word-aligned, you should call addr_alignment_incorrect() and
* return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists
* in the table, you should call name_already_exists() and return -1. If memory
* allocation fails, you should call allocation_failed().And alloction_failed()
* will print error message and exit with error code 1.
*
* Otherwise, you should store the symbol name and address and return 0.
*/
int add_to_table(SymbolTable *table, const char *name, uint32_t addr);
/*
* Convert lui instructions to machine code. Note that for the imm field of lui,
* it may be an immediate number or a symbol and needs to be handled separately.
* Output the instruction to the **OUTPUT**.(Consider using write_inst_hex()).
*
* Return value:
* 0 on success, -1 otherwise.
*
* Arguments:
* opcode: op segment in MIPS machine code
* args: args[0] is the $rt register, and args[1] can be either an imm field or
* a .data segment label. The other cases are illegal and need not be considered
* num_args: length of args array
* addr: Address offset of the current instruction in the file
*/
int write_lui(uint8_t opcode, FILE *output, char **args, size_t num_args, uint32_t addr, SymbolTable *reltbl);
#endif