I am writing a high-level MIPS assembler. I’m having trouble off the bat trying to get fwrite to output correctly. An example assembly file looks like this:
.text
addi $t0,$0,7
addi $t1,$0,1
L1:
beq $t0,$0,DONE
mult $t1,$t0
mflo $t1
addi $t0,$t0,-1
j L1
DONE:
add $a0,$0,$t1
addi $v0,$0,1
syscall
addi $v0,$0,10
syscall
My code should convert these into 32-bit binary instructions and fwrite them to a file to be read by the faux-CPU.
I have a problem off the bat where the 32-bit delimiter at the beginning of the file should be 0xf0f0f0f0. I know you can’t “read” a binary file, but looking at the example file that I have, the delimiter is represented by /360/360/360/360 after being written by fwrite. Why is this and how would I replicate that behavior when I’m writing my own file (as in how do I use fwrite to achieve this to create other binary files)?
The
fwrite(3)for your delimiter would look like this:This writes one object of
delim‘s size to the standard C streamoutput.You can use
od(1)orxxd(1)to “read” binary files:(The
hexer(1)program (in thehexerpackage on Ubuntu) is a lot like an interactivexxd(1), you might find it useful some day.)