Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Richard MacCutchan 5-Dec-21 12:08pm    
Don't use decimal values in your CMP instructions, use character values so the reader can understand what the code is supposed to do. But you do not need all those CMP/JZ sets, since you can check that digits are greater or equal to '0', and less or equal to '9'.
CMP A,'0'
JL Badchar
CMP A,'9'
JG BadChar
...
k5054 5-Dec-21 12:10pm    
What happens when you try to run the program? Does it crash? Does it print the wrong value? Does it print anything? Please let us know what issue you have and we might be able to help you resolve them.
Birddog112 5-Dec-21 12:12pm    
It crashes when I try to to run it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900