Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions assembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ static int add_if_label(uint32_t input_line, char* str, uint32_t byte_offset,
2. If the first token is not a label, treat it as the name of an instruction.
3. Everything after the instruction name should be treated as arguments to
that instruction. If there are more than MAX_ARGS arguments, call
raise_extra_arg_error() and pass in the first extra argument.
raise_extra_arg_error() and pass in the first extra argument. Do not
write that instruction to the output file (eg. don't call write_pass_one())
4. Only one instruction should be present per line. You do not need to do
anything extra to detect this - it should be handled by guideline 3.
5. A line containing only a label is valid. The address of the label should
Expand Down Expand Up @@ -191,8 +192,8 @@ static void close_files(FILE* input, FILE* output) {
int assemble(const char* in_name, const char* tmp_name, const char* out_name) {
FILE *src, *dst;
int err = 0;
SymbolTable* symtbl = create_table();
SymbolTable* reltbl = create_table();
SymbolTable* symtbl = create_table(SYMTBL_UNIQUE_NAME);
SymbolTable* reltbl = create_table(SYMTBL_NON_UNIQUE);

if (in_name) {
printf("Running pass one: %s -> %s\n", in_name, tmp_name);
Expand Down
2 changes: 1 addition & 1 deletion input/p1_errors.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ l1: l2: addiu $t3, $t1, 5 # Multiple labels on one line
ori $t3, $t2, 0xABC

# Things to ignore
addiu $t3, $t5, 3 # invalid register
addiu $t3, $99, 3 # invalid register
ori $t1, $t0, 0xFFFFFFFF # invalid immediate
bne $t0, $t1, not_found # nonexistant label
4 changes: 2 additions & 2 deletions input/p2_errors.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
addiu $t0, $t3, $t3 # not a number
label: jal # no label
ori $t2, $t5, 0xAB # invalid register
ori $t2, $99, 0xAB # invalid register
bne $t0, $t1, not_found # nonexistant label
addiu $t3 $t5 0x80808080 # number too large
addiu $t3 $t2 0x80808080 # number too large

# Can you think of any others?
17 changes: 17 additions & 0 deletions link-in/linker1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.text
1109ffff
1509fffe
08000000
1509fffe
0c000000
1109fffa

.symbol
0 label1
0 label2
8 label3
16 label4

.relocation
8 label2
16 label1
38 changes: 38 additions & 0 deletions link-in/linker2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.text
24040abc
2405000a
0c000000
3c01000a
3422bcde
24080000
11050012
00884821
812a0000
924bfffd
254a0001
00a72025
24080003
0128302a
0128302b
000a5fc0
354b0123
3c0b0214
a12a0000
ad2a8000
8d2b7fff
016a082a
1420ffee
25290001
08000000
03e00008
1564ffea

.symbol
20 myFunc
24 startLoop
64 random
100 endLoop

.relocation
8 myFunc
96 startLoop
14 changes: 14 additions & 0 deletions link-in/linker3A.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
8fa40000
0c000000
244b0000
03e00008
3c01abcd
34221234
03e00008

.symbol
16 func3

.relocation
4 func2
14 changes: 14 additions & 0 deletions link-in/linker3B.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
000420c0
0085082a
14200002
0c000000
2442ffff
03e00008

.symbol
0 func2
20 done

.relocation
12 func3
6 changes: 6 additions & 0 deletions link-out/ref/output1_ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1109ffff
1509fffe
08100000
1509fffe
0c100000
1109fffa
27 changes: 27 additions & 0 deletions link-out/ref/output2_ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
24040abc
2405000a
0c100005
3c01000a
3422bcde
24080000
11050012
00884821
812a0000
924bfffd
254a0001
00a72025
24080003
0128302a
0128302b
000a5fc0
354b0123
3c0b0214
a12a0000
ad2a8000
8d2b7fff
016a082a
1420ffee
25290001
08100006
03e00008
1564ffea
13 changes: 13 additions & 0 deletions link-out/ref/output3_ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
8fa40000
0c100007
244b0000
03e00008
3c01abcd
34221234
03e00008
000420c0
0085082a
14200002
0c100004
2442ffff
03e00008
1 change: 1 addition & 0 deletions linker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mars nc sm linker-src/linker.s pa $@
91 changes: 91 additions & 0 deletions linker-src/file_utils.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# CS 61C Spring 2015 Project 1-2
# file_utils.s
#
# Utilities for opening and closing files

.data
cannot_open_files: .asciiz "Error opening files. Exiting program.\n"

.text
#------------------------------------------------------------------------------
# function open_files()
#------------------------------------------------------------------------------
# Given an array of filenames, opens each file. If a file cannot be opened, exits
# the program.
#
# Arguments:
# $a0 = an array filename strings
# $a1 = length of the array
#
# Returns: an array of file pointers corresponding to the filenames
#------------------------------------------------------------------------------
open_files:
addiu $sp, $sp, -12 # Begin open_files()
sw $s0, 8($sp)
sw $s1, 4($sp)
sw $ra, 0($sp)
move $s0, $a0 # $s0 = file array
move $s1, $a1 # $s1 = number of items
# Alloc space for array:
sll $a0, $a1, 2 # $a0 = # bytes needed for array
li $v0, 9
syscall
move $v1, $v0 # $v1 = array of file ptrs
# Setup flags
li $a1, 0 # flag = read only
li $t0, 0 # $t0 = counter
open_files_begin:
beq $t0, $s1, open_files_done
sll $t1, $t0, 2 # offset in bytes
addu $t2, $s0, $t1 # compute current filename loc
lw $a0, 0($t2)
li $v0, 13
syscall # open input file
blt $v0, 0, open_files_err
addu $t3, $v1, $t1 # compute current file ptr loc
sw $v0, 0($t3) # store file descriptor in array
addiu $t0, $t0, 1
j open_files_begin
open_files_done:
move $v0, $v1 # $v0 = file descriptors
lw $s0, 8($sp)
lw $s1, 4($sp)
lw $ra, 0($sp)
addiu $sp, $sp, 12
jr $ra
open_files_err:
# Error file input failed
move $a0, $v1 # $a0 = file descriptor array
move $a1, $t0 # $a1 = number of files currently open
jal close_files
la $a0, cannot_open_files
li $v0, 4
syscall
li $a0, 1
li $v0, 17 # exit program
syscall # End open_files()

#------------------------------------------------------------------------------
# function close_files()
#------------------------------------------------------------------------------
# Given an array of file pointers, closes all files
#
# Arguments:
# $a0 = an array of file pointers
# $a1 = length of the array
#
# Returns: none
#------------------------------------------------------------------------------
close_files:
move $t0, $a0 # Begin open_files()
sll $a1, $a1, 2
li $t1, 0 # $t1 = counter
cf_begin: slt $t2, $t1, $a1 # $a1 = loop end condition (in bytes)
beq $t2, $0, cf_end
addu $t3, $t0, $t1
lw $a0, 0($t3) # load file descriptor
li $v0, 16
syscall # close file
addiu $t1, $t1, 4
j cf_begin
cf_end: jr $ra # End close_files()
Loading