0 Replies - 555 Views - Last Post: 14 June 2017 - 09:31 AM

#1 popanda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-June 17

Infix to Postfix conversion using masm x86

Posted 14 June 2017 - 09:31 AM

Hi, this is my assignment which needs to evaluate arithmetic operation something like this: 12/3+4, I tried to put this input and it gives me "12". I have stuck at this point, not knowing how to edit my code, and also i wonder why 3 did not appear after I run this, because my code suppose to read until the second operator is reach and go into either CheckAddSub or CheckMulDiv proc. Thank you so much if you could take a look at it. Thank you

INCLUDE C:\Irvine\Irvine32.inc
INCLUDE C:\Irvine\macros.inc
CheckAddSub PROTO, seStr1:PTR BYTE, outStr1:PTR BYTE, pCo:DWORD, pDo:DWORD, outCo1:DWORD
CheckMulDiv PROTO, seStr2:PTR BYTE, outStr2:PTR BYTE, theCo:DWORD, theDo:DWORD, outCo2:DWORD

.data
inputStr BYTE 20 dup(?)    ;input array
outputStr BYTE 20 dup(?)   ;postfix array
sizeSt DWORD ?
secStr BYTE 10 dup(?)    ;to store the operator
count DWORD 0
dStr DWORD 0
outCount DWORD 0
edxCount DWORD 0
val1 DWORD 1

.code
main PROC
    mWrite "Input: "
    mov edx, OFFSET inputStr 
    mov ecx, SIZEOF inputStr - 1
    call ReadString
    mov esi, OFFSET inputStr     ;now the input str has elements
    mov edi, OFFSET outputStr    ;for storing the postfix 
    mov ecx, eax                 ;to loop
    mov sizeSt, eax
    mWrite "the ecx is " 
    call WriteDec
    call Crlf
    mov edx, OFFSET secStr       ;to store operator
    
L1:   
    call Crlf
    mov bl, [esi]
    mWrite "the element : "
    mov al,[esi]
    call WriteChar
    call Crlf
    mWrite "OutCount at main: " 
    mov eax, outCount
    call WriteDec
    call Crlf
    .if bl=='+'|| bl=='-' 
        inc count             
        mWrite "Count at addSub: "        
        mov eax, count
        call WriteDec
        call Crlf

        mWrite "OutCount at addSub: " 
        mov eax, outCount
        call WriteDec
        call Crlf

        mov eax, count
        .if eax > val1        ;when count is 2 mean there is already element in the secStr array so i can do the checking
            mWrite "the addSub"
            call Crlf
            INVOKE CheckAddSub, ADDR secStr, ADDR outputStr, count, dStr, outCount
       
            inc dStr        ;dStr is to store the index number of the secStr array, after the zero index predencency is checked, 
                            ;increment dStr to indicate the index number this current operator in secStr array
            
        .endif
                 
        mov [edx],bl    ;put the operator into secStr
        inc edx
        inc edxCount
               
    .elseif bl=='*'|| bl =='/'
        inc count
        mWrite "Count at mulDiv: "
        mov eax, count
        call WriteDec
        call Crlf

        mWrite "OutCount at mulDiv: " 
        mov eax, outCount
        call WriteDec
        call Crlf

        mov eax, count
        .if eax > val1
            mWrite "the MulDiv"
            call Crlf
            INVOKE CheckMulDiv, ADDR secStr, ADDR outputStr , count, dStr, outCount
     
            inc dStr
        .endif
       
        mov [edx],bl   
        inc edx
        inc edxCount
        
    .else                    ;when it is operand(number), put into output array
        mov [edi], bl
        inc outCount

        mWrite "the output array"
        call Crlf
        
    .endif

    mWrite "edx value : " 
    mov eax, edxCount
    call WriteDec
    call Crlf
    inc edi
    inc esi
    dec ecx
    cmp ecx,0
    jne L1
          
    mov edx, OFFSET outputStr
    mWrite "the LAST postfix string is: "
    call WriteString
    
    INVOKE ExitProcess,0

main ENDP
;-----------------------------------------------------------
CheckAddSub PROC USES edx edi ecx eax ebx esi,
    seStr1:PTR BYTE,      ;secStr
    outStr1:PTR BYTE,     ;outputStr
    pCo:DWORD, 
    pDo:DWORD,
    outCo1:DWORD
;------------------------------------------------------------
  
    lea edx, seStr1      
    lea edi, outStr1
    mov ecx, pDo
    lea eax, outCo1
    mov esi, pCo
   
LW:
    mov bl, [edx+ecx]            ;edx+pDo is the last element in the array, like stack the top element
    .if bl=='+'|| bl=='-'|| bl=='*' || bl=='/'
        mov [edi+eax],bl         ;if one of these operator exists in the secStr array, then put this operator into the outputStr array 
        mov bl,0
        mov [edx+ecx],bl         ;put null in this current index, indicating this element is already pop out from the secStr array    
        inc eax
    .endif
    
    .if ecx!=0
        sub ecx,1
    .endif
    
    dec esi        
    cmp esi,1
    jne LW
    je L5
    

L5: ret 
     
CheckAddSub ENDP

;------------------------------------------------------
CheckMulDiv PROC USES edx edi eax ecx ebx esi,
    seStr2:PTR BYTE, 
    outStr2:PTR BYTE, 
    theCo:DWORD, 
    theDo:DWORD,
    outCo2:DWORD
;-------------------------------------------------------

    lea edx,seStr2
    lea edi,outStr2
    mov ecx,theDo
    lea eax,outCo2
    mov esi,theCo
  
LR:
    mov bl,[edx+ecx]
    .if bl=='*'|| bl=='/'
        mov [edi+eax],bl
        mov bl, 0
        mov [edx+ecx],bl
        inc eax
    .endif
    
    .if ecx!=0
        sub ecx,1
    .endif

    dec esi
    cmp esi, 1
    jne LR
    je L6
    
L6: ret

CheckMulDiv ENDP
END main


Is This A Good Question/Topic? 0
  • +

Page 1 of 1