summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-09 09:07:22 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-09 09:45:30 +0200
commit3487de120ecf98cd78bbc1e4a3a620e955309665 (patch)
treee1e35f89334bdade873d45deed6a0ca4ab0551a5
parentf4c8a4073495fe27473ab13fab1aa0a63773806e (diff)
downloadokeffa-3487de120ecf98cd78bbc1e4a3a620e955309665.tar.gz
mul div galk
-rw-r--r--calc/main.c41
-rw-r--r--makefile2
2 files changed, 40 insertions, 3 deletions
diff --git a/calc/main.c b/calc/main.c
index d5ee16a..e0f2a57 100644
--- a/calc/main.c
+++ b/calc/main.c
@@ -56,6 +56,10 @@ static void want(size_t n){
if(sp+1 < n) e("stack underflow!");
}
+static void mustnotzero(word_t *w){
+ if(fz_zero(w,fl)) e("division by zero!");
+}
+
static void ins_nib(word_t *w,word_t nib){
assert((nib & 0xF) == nib);
word_t of = fz_shl(w,w,fl,4);
@@ -76,6 +80,12 @@ static word_t char_getnib(char c){
else return (word_t)(c-'a')+0xA;
}
+static void puts_stack(size_t p){
+ printf("%6zu ",p);
+ dump_fz(&STACK(p),fl);
+ write_nl();
+}
+
static void op_normal(char c){
switch(c){
wbool_t p;
@@ -183,6 +193,31 @@ static void op_normal(char c){
drop();
break;
+ case '*': //multiply
+ want(2);
+ fz_mul_egypt(&STACK(sp-1),&STACK(sp),&STACK(sp-1),&STACK(sp),fl);
+ break;
+
+ case '\\': //divide, keep quotient and remainder
+ want(2);
+ mustnotzero(&STACK(sp));
+ fz_idiv(&STACK(sp-1),&STACK(sp),&STACK(sp-1),&STACK(sp),fl);
+ break;
+
+ case '/': //divide, keep only quotient
+ want(2);
+ mustnotzero(&STACK(sp));
+ fz_div(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ drop();
+ break;
+
+ case '%': //divide, keep only remainder
+ want(2);
+ mustnotzero(&STACK(sp));
+ fz_mod(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ drop();
+ break;
+
////////////////////////////////////////////////////////////////
//bitwise ops
////////////////////////////////////////////////////////////////
@@ -229,7 +264,8 @@ static void op_normal(char c){
case '#': //print top item and drop it
want(1);
- dump_fz(&STACK(sp),fl);
+ //dump_fz(&STACK(sp),fl);
+ puts_stack(sp);
drop();
break;
@@ -239,7 +275,8 @@ static void op_normal(char c){
case 'Q': //quit with stack trace
for(size_t x = sp;x != STACK_INIT;x--){
- dump_fz(&STACK(x),fl);
+ //dump_fz(&STACK(x),fl);
+ puts_stack(x);
}
exit(0);
break;
diff --git a/makefile b/makefile
index 41662cb..9124cdc 100644
--- a/makefile
+++ b/makefile
@@ -66,7 +66,7 @@ out := $(bin_dir)/$(out)
ifneq ($(run),)
run_cmd := @echo "run $(out)" 1>&2
#run_cmd += && ./$(out) 256 16
- run_cmd += && cat prog/ch4_num prog/ch4_ans | $(out) 256 16
+ run_cmd += && cat prog/ch5_hw1 | $(out) 256 16
endif
ifneq ($(debug),)