Asm Dsl
Title: ASM-DSL
Date: 2017-03-11T00:00:00
Tags: CS231, Assembly, DSL
Authors: Henry Brooks
I have been thinking about all of the boilerplate code that I keep writing in my assembly class and I decide to start looking into creating a simple DSL to assist with my code writing.
Mostly I am looking to simplify code blocks that have easy c++
analogs. Specifically cin
, cout
, and assignment (=
).
Currently I am writing code like this
example.asm
...
#------ cin << $s0
li $v0, 5
syscall
add $v0, $s0, $0
#------ $s1 = $s0
add $s1, $s0, $0
#------ cout << $s1
li $v0, 1
add $a0, $s1, $0
syscall
...
To get results for code like this in c++
example.cpp
...
int s0, s1;
cin >> s0;
s1 = s0;
cout << s1;
...
I recognize that there are some actions that need a full compiler to implement however, I think that I can implement this much in typed/racket with what I already know. I’m choosing to use typed/racket because I want to experiment with using it and I think it will come in useful if I continue to expand the scope of the code.
ASM-DSL.rkt
#lang typed/racket
;; Save register values
(define func "$v0")
(define return "$v0")
(define input "$s0")
(define arg "$a0")
(define zero "$0")
;; Buildin MIPS instructions
(define (comment [s : String])
(display (string-append "#------ " s "\n")))
(define (add [x : String] [y : String] [z : String])
(display (string-append "\tadd\t"
x ", "
y ", "
z "\n")))
(define (li [x : String] [y : Integer])
(display (string-append "\tli\t"
x ", "
(number->string y) "\n")))
(define (syscall)
(display "\tsyscall\n"))
;; Macros I built
(define (set-reg [x : String] [y : String])
(add x y zero))
(define (cout-int [x : String])
(comment (string-append "cout << " x))
(li func 1)
(set-reg arg x)
(syscall)
(display "\n"))
(define (cin-int [x : String])
(comment (string-append "cin << " x))
(li func 5)
(syscall)
(add return x zero)
(display "\n"))
;; Mips code
(cin-int "$s0")
(set-reg "$s1" "$s0")
(cout-int "$s1")
output.asm
#------ cin << $s0
li $v0, 5
syscall
add $v0, $s0, $0
add $s1, $s0, $0
#------ cout << $s1
li $v0, 1
add $a0, $s1, $0
syscall