Added Equals '='

This commit is contained in:
MCorange 2023-03-13 16:23:40 +02:00
parent e053d7bef3
commit f2b45e343c
9 changed files with 28 additions and 69 deletions

BIN
a

Binary file not shown.

67
a.nasm
View File

@ -1,67 +0,0 @@
global _start
segment .text
print:
mov r9, -3689348814741910323
sub rsp, 40
mov BYTE [rsp+31], 10
lea rcx, [rsp+30]
.L2:
mov rax, rdi
lea r8, [rsp+32]
mul r9
mov rax, rdi
sub r8, rcx
shr rdx, 3
lea rsi, [rdx+rdx*4]
add rsi, rsi
sub rax, rsi
add eax, 48
mov BYTE [rcx], al
mov rax, rdi
mov rdi, rdx
mov rdx, rcx
sub rcx, 1
cmp rax, 9
ja .L2
lea rax, [rsp+32]
mov edi, 1
sub rdx, rax
xor eax, eax
lea rsi, [rsp+32+rdx]
mov rdx, r8
mov rax, 1
syscall
add rsp, 40
ret
_start:
; -- PUSH 35
mov rax, 35
push rax
; -- PUSH 34
mov rax, 34
push rax
; -- PLUS
pop rax
pop rbx
add rax, rbx
push rax
; -- PRINT
pop rdi
call print
; -- PUSH 500
mov rax, 500
push rax
; -- PUSH 80
mov rax, 80
push rax
; -- MINUS
pop rax
pop rbx
sub rbx, rax
push rbx
; -- PRINT
pop rdi
call print
mov rax, 60
mov rdi, 0
syscall

BIN
a.o

Binary file not shown.

View File

@ -81,6 +81,17 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " sub rbx, rax")?; writeln!(writer, " sub rbx, rax")?;
writeln!(writer, " push rbx")?; writeln!(writer, " push rbx")?;
}, },
OpType::Equals => {
writeln!(writer, " ; -- EQUALS")?;
writeln!(writer, " mov rcx, 0")?;
writeln!(writer, " mov rdx, 1")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rbx")?;
writeln!(writer, " cmp rax, rbx")?;
writeln!(writer, " cmove rcx, rdx")?;
writeln!(writer, " push rcx")?;
},
OpType::Print => { OpType::Print => {
writeln!(writer, " ; -- PRINT")?; writeln!(writer, " ; -- PRINT")?;
writeln!(writer, " pop rdi")?; writeln!(writer, " pop rdi")?;

View File

@ -5,9 +5,16 @@ pub enum OpType {
Pop, Pop,
Minus, Minus,
Plus, Plus,
Equals,
Print Print
} }
// #[derive(Debug)]
// pub enum OpType {
// }
#[derive(Debug)] #[derive(Debug)]
pub struct Operator { pub struct Operator {
pub typ: OpType, pub typ: OpType,

View File

@ -30,6 +30,13 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let b = stack_pop(&mut stack)?; let b = stack_pop(&mut stack)?;
stack.push(b - a); stack.push(b - a);
}, },
OpType::Equals => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
stack.push((a == b) as i32);
},
OpType::Print => { OpType::Print => {
let a = stack_pop(&mut stack)?; let a = stack_pop(&mut stack)?;
println!("{a}"); println!("{a}");

View File

@ -23,11 +23,12 @@ impl Parser {
let num = t.parse::<i32>().unwrap(); let num = t.parse::<i32>().unwrap();
tokens.push(Operator::new(OpType::Push, num)); tokens.push(Operator::new(OpType::Push, num));
}, },
"pop" => tokens.push(Operator::new(OpType::Pop, 0)), "pop" => tokens.push(Operator::new(OpType::Pop, 0)),
"+" => tokens.push(Operator::new(OpType::Plus, 0)), "+" => tokens.push(Operator::new(OpType::Plus, 0)),
"-" => tokens.push(Operator::new(OpType::Minus, 0)), "-" => tokens.push(Operator::new(OpType::Minus, 0)),
"print" => tokens.push(Operator::new(OpType::Print, 0)), "print" => tokens.push(Operator::new(OpType::Print, 0)),
"=" => tokens.push(Operator::new(OpType::Equals, 0)),
t => { t => {

View File

@ -1,3 +1,3 @@
35 34 + printas 35 34 + 69 = print
500 80 - print 500 80 - print

BIN
x86_64-intel-manual.pdf Normal file

Binary file not shown.