Recitation Material of Engineering an Assembler June 11, 2013 Main Purpose of HW3 • The main purpose of hw3 is to give a chance implementing macro function and a linker. • Code should be written in MIPS, and since it is too complicate to implement in short time, we have built a package to support ~~~ Task 1 • First, download and take a look at the fibonacci.f file. In order to compile it, you need an external function called "fibo". Therefore, you will implement this "fibo" function in a file called "func-fibo.f". After you finish this work, you can compile your code using following command line: • gfortran-4.2 -o fibonacci fibonacci.f func-fibo.f • you can use g77 to replace gfortran of course. Fibonacci.f file program main integer Fn_1,Fn,n,val,fibo read(*,*) n Fn_1=0 Fn=1 val=0 if ( n .EQ. 0 ) then val=0 elseif ( n .EQ. 1 ) then val=1 elseif ( n .GE. 2 ) then do 10 i=2, n val=fibo( Fn_1, Fn) Fn_1=Fn Fn=val 10 continue endif write(*,*) val stop end Task 2 • In task2, you assume yourself as John Backus. You just finished implementing IBM's assembly language, and you knew in companies such as Remington Rand, scientist like Grace Hopper already implemented assembly languages, and they even developed some "higher-level" language using primitive compiler like "A-0 System". What you need to do is to summarize your experience developing assembly language, and produce higher-level programming language called "Fortran". The first step toward the goal, is to reuse some existing code. Because writing code in assembly is hard and therefore reusing existing code must be one of functionalities in this new language "Fortran". What you need to do: write a macro file called "macro-fibo.f” , and read the file into your new compiler, translate it into assembly language and output compiled file in MIPS assembly language format. What is the macro file • #define reduce( *1, *2 ) sub reduce, *1, *2 What we need to do: replace the variable name and macro name with corresponding register name in MIPS. What is in package? • You can use QtSpim to load "program-main.s" file. This program will read "fibonacci.f", and output "second_mips_fortran.s" file. • What you need to do is like what you have done in Task1, add some code to make this fibonacci code works. If you successfully add some code, this file can be compiled using gfortran or g77. In the sense, you have included a new functionality called "macroinstruction" for "Fortran" language. What is in second_mips_fortran.s ? • • .globl main main: li $2, 5 syscall add $7, $2, 0 li $5, 0 li $6, 1 li $8, 0 li $24, 0 bne $7, $24, elseif1 li $8, 0 j finish elseif1: li $24, 1 bne $7, $24, elseif2 li $8, 1 j finish elseif2: li $24, 2 blt $7, $24, elseif3 li $25, 2 begin: beq $25, $7, endloop add $8, $5, $6 add $5, $6, 0 add $6, $8, 0 add $25, $25, 1 j begin endloop: elseif3: finish: li $2, 1 add $4, $8, 0 syscall nop nop nop li $2, 10 syscall Recall Fibonacci.f file program main integer Fn_1,Fn,n,val,fibo read(*,*) n Fn_1=0 Fn=1 val=0 if ( n .EQ. 0 ) then val=0 elseif ( n .EQ. 1 ) then val=1 elseif ( n .GE. 2 ) then do 10 i=2, n val=fibo( Fn_1, Fn) Fn_1=Fn Fn=val 10 continue endif write(*,*) val stop end Take a look at program-main.s • Notice: there are only 6 registers available, they are: $s0, $s1, $s2, $s3, $s4, $s5. If you want to use more registers, be sure to save them on stack before using them, and recover them after using them. Register $s7 can not be used, because it is the offset pointer of translation buffer, see following example: li $s2, 0x6f # load ascii character 'o' sb $s2, translation_buffer($s7) add $s7, $s7, 1 sb $s2, translation_buffer($s7) add $s7, $s7, 1 sb $s2, translation_buffer($s7) add $s7, $s7, 1 sb $t4, translation_buffer($s7) add $s7, $s7, 1 # save ascii character 'o' to translation buffer # add the offset by 1 li $s2, 0x70 # load ascii character 'p' # save ascii character 'p' to translation buffer # add the offset by 1 li $s2, 0x3a # load ascii character ':' # save ascii character ':' to translation buffer # add the offset by 1 # save ascii new line character '0x0a' to translation buffer # add the offset by 1 Above example shows how to save a word "op:" with a new line symbol to translation buffer Don’t forget to put newline symbol! • When you save MIPS instructions to translation_buffer, separate line using 0x0a, or a new line symbol for MIPS assembler to segment lines.