CS401_2 _BC100401970_

Document Sample
CS401_2 _BC100401970_ Powered By Docstoc
					                               CS401

                               ASSIGNMENT # 2

                               BC 100401970

Question # 1: (a)

Assemble the following instructions and show the changes in given Accumulator register and
flags after each instruction execution.

Answer:

MOV AL, 8Bh ;       CF = 0, SF = 0, PF = 0, ZF = 0



ADD AL, 22h ;       CF = 0, SF = 0, PF = 0, ZF = 0



AND Al, 1000101b ; CF = 0, SF =1 PF = 0, ZF = 0



OR Al, 9Ah ;        CF = 0, SF = 0, PF = 1, ZF = 0



XOR AL, 10101011b ; CF = 0, SF = 1, PF = 1, ZF = 1



TEST AL, 2 ;            CF = 0, SF = 0, PF = 0, ZF = 0



CMP AL, 58 ;            CF = 0, SF = 0, PF = 1, ZF = 1



ADC AL, 11110000b ; CF = 1, SF = 1, PF = 1, ZF = 0



SHR AL,2 ;             CF = 1, SF = 0, PF = 0, ZF = 0
Question # 1: (b)

Write a subroutine that can calculate the sum of an array and also save the result into DX.

(Array’s length is 20)

Answer:

#include "msp.h"

PUBLIC suma_sp

RSEG CODE

suma_sp:

; save the registers on stack
PUSH        R7            ; the temporal sum
PUSH        R6            ; the array length (Array’s length is 20)

PUSH       R4         ; pointer to array
CLR         R7
MOV        10(SP), R6 ; retrieve array length
MOV        12(SP), R4

lnext:    ADD   @R4+, R7
          DEC          R6
          JNZ          lnext
          MOV   8(SP), R4               ; get id from the stack
          BIT   #1,     R4              ; display on P1&P2
          JNZ    lp34                   ; it's P3&P4
          MOV.B R7, P1OUT
          SWPB          R7
          MOV.B R7, P2OUT
         JMP     lend

lp34:    MOV.B       R7, P3OUT
         SWPB               R7
         MOV.B       R7, P4OUT

lend:    POP                 R4         ; restore R4
         POP                 R6
         POP                 R7
         RET
         END

Question # 2:
Write an assembly language code to calculate the factorial of “7” using a subroutine and also
attach the final snapshot of your AFD window

Answer:

DATA SEGMENT
 NO DW 7
DATA ENDS

CODE SEGMENT
   FACTO PROC NEAR
        ASSUME CS:CODE
        MOV AX,0000
        POP BX
        POP AX
        PUSH BX
        MOV BL,AL
        DEC BL

       L1:MUL BL
        DEC BL
        CMP BL,00
        JNE L1
  L2:RET
    FACTO ENDP
    ASSUME DS:DATA,CS:CODE
START:
    MOV AX,DATA
    MOV DS,AX
    PUSH NO
    CALL FACTO
    MOV BH,00
    MOV AH,4CH
    INT 21H
CODE ENDS
END START

;------
;OUTPUT
;------

     -G CS: 0020

     AX=0078 BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
     DS=0BA4 ES=0B94 SS=0BA4 CS=0BA5 IP=0020 NV UP EI PL ZR NA PE NC
     0BA5:0020 B700   MOV BH,00
     -Q

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:4
posted:7/31/2012
language:English
pages:4
Description: virtual university solved assignment of many subject spring 2012