add: sqrt, neg instructions, fix: pushing register with value 0
This commit is contained in:
2
Makefile
2
Makefile
@@ -7,7 +7,7 @@ OBJ = $(SRC:.c=.o)
|
||||
NAME = pasm
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -Iinclude -s -Os -fno-ident -fno-asynchronous-unwind-tables
|
||||
CLIBS =
|
||||
CLIBS = -lm
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
|
||||
@@ -71,6 +71,8 @@ The syntax is very close to x86 Intel Assembly. Here is a list of the operands a
|
||||
| **sub** a1, a2 | Substract a2 from a1 | no |
|
||||
| **mul** a1, a2 | Multiplies a2 with a1 | no |
|
||||
| **div** a1, a2 | Divides a2 from a1 | no |
|
||||
| **sqrt** a1 | computes sqrt(a1) | no |
|
||||
| **neg** a1 | a1 = -a1 | no |
|
||||
| **and** a1, a2 | Performs a bitwise AND between a1 and a2. Stores the result in eax. | yes |
|
||||
| **xor** a1, a2 | Performs a XOR opration between a1 and a2. Stores the result in eax.| yes |
|
||||
| **mov** a1, a2 | Moves the value stored in a2 in a1 | no |
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "instructions.h"
|
||||
#include "interpreter_states.h"
|
||||
#include "api.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -25,7 +26,7 @@ bool check_args(s_arguments *args, int num_in_first, int num_args) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((!is_num(args->arg2) && !is_reg(args->arg2))) {
|
||||
if ((!is_num(args->arg2) && !is_reg(args->arg2)) && num_args == 2) {
|
||||
state->last_check_args_code = ARG2_WRONG;
|
||||
return false;
|
||||
}
|
||||
@@ -189,6 +190,23 @@ void add() {
|
||||
*get_reg(state->args->arg1) += get_value(state->args->arg2);
|
||||
}
|
||||
|
||||
|
||||
void _sqrt() {
|
||||
if (!check_args(state->args, 0, 1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
*get_reg(state->args->arg1) = sqrt(get_value(state->args->arg1));
|
||||
}
|
||||
|
||||
void neg() {
|
||||
if (!check_args(state->args, 0, 1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
*get_reg(state->args->arg1) = -get_value(state->args->arg1);
|
||||
}
|
||||
|
||||
void mul() {
|
||||
if (!check_args(state->args, 0, 2)) {
|
||||
return;
|
||||
@@ -230,7 +248,7 @@ void push() {
|
||||
}
|
||||
|
||||
int value = get_value(state->args->arg1);
|
||||
if (value == 0) {
|
||||
if (value == 0 && !is_reg(state->args->arg1)) {
|
||||
if (state->args->arg1[0] == '\\') {
|
||||
switch (state->args->arg1[1]) {
|
||||
case 'n':
|
||||
|
||||
@@ -11,6 +11,7 @@ typedef struct command_s {
|
||||
bool is_reg(char* arg);
|
||||
bool check_args(s_arguments *args, int num_in_first, int num_args);
|
||||
int* get_reg(char* arg);
|
||||
int get_value(char* arg);
|
||||
|
||||
void add();
|
||||
void sub();
|
||||
@@ -29,6 +30,8 @@ void ret();
|
||||
void pop();
|
||||
void push();
|
||||
void call();
|
||||
void neg();
|
||||
void _sqrt();
|
||||
void _and();
|
||||
void _xor();
|
||||
void end();
|
||||
@@ -51,6 +54,8 @@ static const command_t command_map[] = {
|
||||
{.command = "pop", .fptr = pop},
|
||||
{.command = "push", .fptr = push},
|
||||
{.command = "call", .fptr = call},
|
||||
{.command = "sqrt", .fptr = _sqrt},
|
||||
{.command = "neg", .fptr = neg},
|
||||
{.command = "and", .fptr = _and},
|
||||
{.command = "xor", .fptr = _xor},
|
||||
{.command = "end", .fptr = end},
|
||||
|
||||
Reference in New Issue
Block a user