CSci250 Assembly Language Programming: Homework 4 Solutions
Due date (firm): Monday, November 10, 2008 in class
Each homework may have a different weight, which depends on its level of difficulty.
Absolutely no copying others' work

  1. Write a program that
    1. prompts the user for an integer postfix arithmetic expression, which may include operators, ‘+’, ‘-’ and ‘=’, and one-digit operands, such as “62-7+=”
    2. calculates the postfix expression by using the runtime stack, and
    3. displays the result.

    For example, “35-86+-= -16” and “51-5+3-37+-= -4.” No error checking is required.     (40%)
    Ans>
       INCLUDE Irvine32.inc
       .code
       main PROC
       L1: call   ReadChar
           call   WriteChar
           cmp    al, '+'
           jne    L2
           pop    ebx
           pop    eax
           add    eax, ebx
           push   eax
           jmp    L1
    
       L2: cmp    al, '-'
           jne    L3
           pop    ebx
           pop    eax
           sub    eax, ebx
           push   eax
           jmp    L1
               
       L3: cmp    al, '='
           jne    L4
           pop    eax
           call   WriteInt
           jmp    L5
    
       L4: sub    al, '0'
           movsx  eax, al
           push   eax
           jmp    L1
        
       L5: call   Crlf
           call   Crlf
           exit
       main ENDP
       END main











  2. In the following instruction sequence, show the changed value of AL where indicated, in binary:     (16%)
         mov  al, 10011001b
         and  al, 0C5h           ; AL =  81h = 10000001b
         mov  al, 92h
         not  al                 ; AL =  6Dh = 01101101b
         mov  al, 10010010b
         or   al, 6Dh            ; AL = 0FFh = 11111111b
         mov  al, 0C7h
         xor  al, 5Eh            ; AL =  99h = 10011001b


  3. In the following instruction sequence, show the values of the Carry, Zero, and Sign flags where indicated:     (18%)
         mov   al, 01101010b
         test  al, 0B5h          ; CF = 0      ZF = 0      SF = 0
         mov   al, 80h
         cmp   al, 1             ; CF = 0      ZF = 0      SF = 0
         mov   al, 0FFh
         cmp   al, -1            ; CF = 0      ZF = 1      SF = 0


  4. Write a single instruction that reverses the high 4 bits of AL and does not change the low 4 bits.     (12%)
    Ans>
       xor   al, 0F0h
        

  5. Write instructions that clear the bits 1, 3, 5, and 7 in AL. If the destination operand is greater or equal to 1, jump to L3. Otherwise, jump to label L4.     (14%)
    Ans>
       and   al, 55h
       cmp   al, 1
       jge   L3
       jmp   L4