If the input is a number, it should be converted from a character value to a numeric value and that value should be pushed onto the stack.
If the input is either "+" or "-", the top two entries on the stack are to be popped to registers, the operation performed and the result pushed back on the stack
At the end of the equation, the top of the stack should be popped to register "C" and the program halted.
What I have tried:
JMP start
hello: DB "A2345+-" ; Variable
DB 0 ; String terminator
start:
MOV C, hello ; Point to var
MOV D, 232 ; Point to output
CALL printNum
HLT ; Stop execution
printNum: ; print(C:*from, D:*to)
PUSH A
PUSH B
MOV B, 0
.loop:
MOV A, [C] ; Get char from var
CMP A, 48
JZ GoodChar
CMP A, 49
JZ GoodChar
CMP A, 50
JZ GoodChar
CMP A, 51
JZ GoodChar
CMP A, 52
JZ GoodChar
CMP A, 53
JZ GoodChar
CMP A, 54
JZ GoodChar
CMP A, 55
JZ GoodChar
CMP A, 56
JZ GoodChar
CMP A, 57
JZ GoodChar
CMP A, 43 ; COMPARE TO ASCII +
JZ GoodChar
CMP A, 45
JZ GoodChar
JMP BadChar
GoodChar:
MOV [D], A ; Write to output
INC C
INC D
CMP B, [C] ; Check if end
JNZ .loop ; jump if not
POP B
POP A
RET
BadChar:
push C
MOV C, Error
MOV D, 232 ; Point to output
call print
POP C
hlt
print: ; print(C:*from, D:*to)
PUSH A
PUSH B
MOV B, 0
MOV [D], A
MOV A, 32
MOV [D], A
.loop2:
MOV A, [C] ; Get char from var
MOV [D], A ; Write to output
INC C
INC D
CMP B, [C] ; Check if end
JNZ .loop2 ; jump if not
POP B
POP A
RET
(This is the what I have tried)
Check if it is a char:
movzx eax, byte [rdi] ; start with the first digit
sub eax, '0' ; convert from ASCII to number
cmp al, 9 ; check that it's a decimal digit [0..9]
jbe .loop_entry ; too low -> wraps to high value, fails unsigned compare check
; else: bad first digit: return 0
xor eax,eax
ret