add abs value

This commit is contained in:
Mylloon 2022-12-22 16:51:15 +01:00
parent 122c244abc
commit abf30e8bda
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 6 additions and 2 deletions

View file

@ -18,6 +18,7 @@ let _types_ =
; "puti", Func_t (Void_t, [ Int_t ])
; "puts", Func_t (Void_t, [ Str_t ])
; "geti", Func_t (Int_t, [])
; "abs", Func_t (Int_t, [ Int_t ])
])
;;
@ -38,5 +39,6 @@ let builtins =
; "puti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_int); Syscall ]
; "puts", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_str); Syscall ]
; "geti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.read_int); Syscall ]
; "abs", [ Lw (T0, Mem (SP, 0)); Abs (V0, T0) ]
]
;;

View file

@ -47,6 +47,7 @@ type instr =
| Mul of reg * reg * reg
| Sub of reg * reg * reg
| Div of reg * reg * reg
| Abs of reg * reg
| Seq of reg * reg * reg
| Sge of reg * reg * reg
| Sgt of reg * reg * reg
@ -131,6 +132,7 @@ let fmt_instr ?(indent = " ") = function
Printf.sprintf "%ssub %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
| Div (rd, rs, rt) ->
Printf.sprintf "%sdiv %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
| Abs (rd, rs) -> Printf.sprintf "%sabs %s, %s" indent (fmt_reg rd) (fmt_reg rs)
| Seq (rd, rs, rt) ->
Printf.sprintf "%sseq %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
| Sge (rd, rs, rt) ->

View file

@ -1,4 +1,4 @@
void main () {
int res = 13 * 100 + 20 - 8;
res = res * 2 / 2;
int res = 13 * -100 - 20 + 8;
res = abs(res * 2) / 2;
}