29.9k views
2 votes
1. Allocate 1024 bytes of dynamic memory and save the pointer to the area. 2. The main program is a loop in which you ask the user for a filename. If they enter nothing for the filename, exit the program. Otherwise: a. Open the file for reading. If the file does not exist, print an error message and terminate the program. b. Read the file into an input buffer space of 1024 bytes. c. Close the file.

User Victrnava
by
6.3k points

1 Answer

4 votes

Answer:

Macros: Trying this .

.data

.macro print_int(%x)

li $v0,1

li. $a0,%x

syscall

.end_macro

.macro print_string(%s)

li $v0,4

li $a0,%s

syscall

.end_macro

.macro print_character(%c)

.li $v0,'1'

.li $a0,%c

syscall

.end_macro

fin: .asciiz "l.dat"

buffer: .asciiz 1024 ""

.macro open_file(fin)

.li $v0,13

.la $a0,fin

.li $a1,0

.li $a2,0

syscall

move $s6,$v0

.end_macro

.macro read_file(fin)

.$v0,14

move $a0,$s6

.la $a1,buffer

.li $a3,1024

syscall

.end_macro

.macro close_file(fin)

.li $v0,16

move $a0,$s6

syscall

.end_macro

_main:

print_string("Hello")

loop:

la $a0,print_string("enter filename")

li $v0,4

read:

beq $v0,$zero,read

open:

la $a0,open_file(fin)

li $v0,4

beq $v0,$zero,exit

end_loop:

exit:

li $v0,10

syscall

Hope this helps.By using all the macros as used for open_file along with $a(1--10),$v(1--10), we can easily do all functionality.

User Shalafi
by
6.5k points