Sunday, April 10, 2011

Final - Dividing Bit Patterns + manupulate the extraction of the 8-bits and shift right + addition and substraction of the bits + and, or, xor. + floating point operations + binary scientific notations formatting


; Title: CISS 360 Finals
;------------------------------------------------------------
; Description: This program has different parts of which
; the first portion asks us to be familiar with dividing
; bit patterns and manupulating the quotient and remainder
; The second part makes us manupulate the extraction of the 8-bits
; and shift right it and putting it in the DWORD
; The third part of the problem is to make us be familiar with
; addition and substraction of the bits
; The fourth part of the problem is to make us experienced with
; and, or, xor.
; The fifth part of the problem is to be familiar with the
; floating point operations.
; The sixth part of the problem is to make us experienced with the
; binary scientific notations formatting.
;------------------------------------------------------------
; Programmer: Prajwal Shrestha
; Professor: Ronald J. Hart
;------------------------------------------------------------
; Date/Time: Tuesday, March 01, 2011 12:27 AM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h  ; header file for console input/output
.STACK 4096   ; reserve 4096-byte (1024 DWORD) stack
;------------------------------------------------------------
;                   Input/Output Facility                  
;------------------------------------------------------------
; Macro     Operand
;
; put_ch    DWORD/Reg     (Displays character in parameter)
; get_ch    None          (Loads character from keyboard into EAX)
; put_str   BYTE          (Displays string beginning at given address)
; put_i     DWORD/Reg     (Displays signed integer value in parameter)
; put_u     DWORD/Reg     (Displays unsigned integer value in parameter)
; put_fp    REAL4/Reg     (Displays float value in parameter)
; get_i     DWORD         (Loads signed integer entered at keyboard into parameter)
; get_u     DWORD         (Loads unsigned integer entered at keyboard into parameter)
; get_fp    REAL4         (Loads float entered at keyboard into parameter)
; get_str   BYTE          (Loads string entered at keyboard into parameter)
;------------------------------------------------------------

.DATA   ;****************************************************
        ;*                  Data Section                    *
        ;****************************************************
lable1 BYTE 0h, 0h, 0ebh, 53h
lable2 BYTE 36h, 2fh, 0adh, 0d5h
lable3 BYTE 0b6h, 2ch, 0d1h, 35h
lable4 BYTE 0ffh, 52h, 0adh, 36h

praj REAL4 28.43

part1 BYTE "Part 1 -----------------------------------", 0
part2 BYTE "Part 2 -----------------------------------", 0
part3 BYTE "Part 3 -----------------------------------", 0
part4 BYTE "Part 4 -----------------------------------", 0
part5 BYTE "Part 5 -----------------------------------", 0
part6 BYTE "Part 6 -----------------------------------", 0
space BYTE "   ", 0

part1_fpop DWORD ?
part2_fpop DWORD ?
part2a_op DWORD ?
part2b_op DWORD ?
part3_fpop DWORD ?

part4a_op DWORD ?
part4b_op DWORD ?
part4c_op DWORD ?
part4_fpop DWORD ?

part5_fpop DWORD ?
part6_fpop DWORD ?

val_one DWORD 1000000.0
val_two DWORD 10.0

newline DWORD 10 ;

.CODE   ;****************************************************
        ;*                  Code Section                    *
        ;****************************************************

_MainProc PROC

    rerun_main:
; ||--#PART ONE#--||
lea esi, lable1 ; getting the effective address of lable1 and putting it in esi
mov ecx, DWORD PTR [esi] ; moving the bit pattern in to ecx

push ecx ; pushing eax
put_ch newline ; starts the newline for display puropses
put_str part1 ; diaplying the part1 message on display

put_ch newline ; starts the newline for display puropses
put_ch newline ; starts the newline for display puropses
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits procedure so that we can see the bit pattern
add esp, 4 ; adding 4 to esp

mov ebx, ecx ; moving ecx in to eax
and ebx, 0ff000000h ; extracting first 8 bits only
        shr ebx, 24 ; shifting it right 24 positions

mov eax, ecx ; moving ecx to ebx
and eax, 00ff0000h ; extracting 8 bits only
shr eax, 16 ; shifting 16 position right

cdq ; dividing by eax
idiv ebx ; dividing by eax ie. A
shl eax, 8 ; shift left 8 position

or ecx, eax ; concanitating ecx and eax
or ecx, edx ; concanitating ecx and edx

push ecx ; pushing ecx
put_str space ; for display purpose
call display_bits ; calling display bits
add esp, 4 ; adding 4 to esp
mov part1_fpop, ecx ; moving ecx to DWORD
put_str space ; for display
put_fp part1_fpop ; displaying floating point stored in DWORD
put_ch newline ; For a new line


; ||--##PART TWO A##--||

lea esi, lable2 ; getting the effective address of lable2 and putting it in esi
mov ecx, DWORD PTR [esi] ; moving the bit pattern in to exc

mov eax, ecx ; moving ecx to eax to preserve ecx
push eax ; pushing eax
put_ch newline ; starts the newline for display puropses
put_str part2 ; diaplying the part1 message on display

put_ch newline ; starts the newline for display puropses
put_ch newline ; starts the newline for display puropses
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits procedure so that we can see the bit pattern
add esp, 4 ; adding 4 to esp

mov edx, ecx ; moving ecx to edx
and edx, 0ff0000ffh ; extracting E and H from the lable2 (ecx) bit pattern
shr edx, 4 ; shifting the bit positions 4 bits right most as according to question

mov ebx, ecx ; moving ebx to edx
and ebx, 00ffff00h ; AND operation to get the 16 bits
or edx, ebx ; replacing E and H with the result

mov part2a_op, edx ; moving the edx bit pattern to a DWORD


; ||--##PART TWO B##--||

mov edx, ecx ; moving ecx to edx
and edx, 000ffff00h ; extracting F and G from the lable2 (ecx) bit pattern
shl edx, 4 ; shifting the bit positions 4 bits right most as according to question

mov ebx, ecx ; moving ecx to ebx
and ebx, 0ff0000ffh ; getting the bits patterns from first 8 bits and last 8 bits
or edx, ebx ; replacing F and G with the result

mov part2b_op, edx ; moving the edx bit pattern to a DWORD


mov ebx, part2a_op ; moving DWORD value to ebx
mov edx, part2b_op ; moving DWORD value to edx

and ebx, 0ff0000ffh ; AND operation with the bits pattern of ebx to get first and last 8-bits
and edx, 00ffff00h ; AND operation with the bits pattern of edx to get the second & third 8 bits
or ebx, edx ; concanitating edx and ebx registers


push ebx ; pushing edx to stack
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display bits
add esp, 4 ; adding 4 to esp

mov part2_fpop, edx ; moving edx value to DWORD
put_str space ; for the display formatting
put_fp part2_fpop ; displaying the floating point value for the resultant
put_ch newline ; for the newline


; ||--###PART THREE###--||

lea esi, lable3 ; getting the effective address of lable3 and putting it in esi
mov ecx, DWORD PTR [esi] ; moving the bit pattern in to exc


mov eax, ecx ; moving ecx to eax to preserve ecx
push eax ; pushing eax
put_ch newline ; starts the newline for display puropses
put_str part3 ; diaplying the part1 message on display

put_ch newline ; starts the newline for display puropses
put_ch newline ; starts the newline for display puropses
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits procedure so that we can see the bit pattern
add esp, 4 ; adding 4 to esp

mov eax, ecx ; moving eax with ecx bit pattern
and eax, 0ff000000h ; extracting i only from the whole
shr eax, 24 ; shifting actual bits to right most place and filing others with "0"

mov ebx, ecx ; moving eax with ecx bit pattern
and ebx, 0000ff00h ; extracting k only from the whole
shr ebx, 8 ; shifting actual bits to right most place and filing others with "0"

add eax, ebx ; adding eax and ebx is sum of bytes I and K
shl eax, 24 ; shifting left 24 position

mov ebx, ecx ; moving ecx to ebx
and ebx, 00ffffffh ; AND operation with eax to not include first 8-bits
or eax, ebx ; replacing I with sum

; substraction
mov ebx, ecx ; moving ecx to ebx
and ebx, 00ff0000h ; extracting J only
shr ebx, 16 ; shifting actual bits to right most place and filing others with "0"

mov edx, ecx ; moving ecx to edx
and edx, 000000ffh ; extracting J only

sub ebx, edx ; subtracting ebx and edx
shl ebx, 16 ; shifting left 16 position

mov edx, eax ; moving eax to edx
and edx, 0ff00ffffh ; AND operation with edx to not include second 8-bits

or edx, ebx ; concanitating edx and ebx
put_str space ; formatting as a tab to make display look better
push edx ; pusing edx
call display_bits ; calling display bits
add esp, 4 ; adding 4 to esp
mov part3_fpop, edx ; moving edx to DWORD

put_str space ; formatting as a tab to make display look better
put_fp part3_fpop; ; flaoting point output
put_ch newline ; display newline

; ||--####PART FOUR####--||
lea esi, lable4 ; getting the effective address of lable4 and putting it in esi
mov ecx, DWORD PTR [esi] ; moving the bit pattern in to exc

mov eax, ecx ; moving ecx to eax to preserve ecx
push eax ; pushing eax
put_ch newline ; starts the newline for display puropses
put_str part4 ; diaplying the part1 message on display

put_ch newline ; starts the newline for display puropses
put_ch newline ; starts the newline for display puropses
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits procedure so that we can see the bit pattern
add esp, 4 ; adding 4 to esp

; Replacing byte M with the "and" of bytes M and N
mov edx, ecx ; moving ecx to edx register
and edx, 0ff000000h ; extracting M, the first 8-bits from ecx

mov ebx, ecx ; moving ecx to ebx register
and ebx, 00ff0000h ; extracting N, the second 8-bits from ebx
shl ebx, 8 ; shifting 8 bits position left

and edx, ebx ; performing "and" of bytes M and N

and eax, 00ffffffh ; since we need to replace byte M with AND of edx(M) and ebx(N) putting 00 for the first 8 bits
or eax, edx ; concanitating bits values of two register


; Replacing the byte N with the "or" if bytes N and O
mov edx, ecx ; moving ecx to edx for manupulation of ecx and ecx being saved as it is
and edx, 00ff0000h ; extracting N, the second 8-bits from edx

mov ebx, ecx ; moving ecx to ebx for manupulation of ecx and ecx being saved as it is
and ebx, 0000ff00h ; extracting o, the third 8-bits from edx
shl ebx, 8 ; shifting 8 bits position left

or edx, ebx ; performing OR of N and O ie edx and ebx and saving it to edx


and eax, 0ff00ffffh ; purpose: to get all the values of the other register but take the value of second 8-bits from the other register
or eax, edx ; since we need to replace byte N with OR of bytes N and O


; replacing byte P with the "XOR" of byte P with itself
mov edx, ecx ; moving ecx to edx
and edx, 000000ffh ; extracting P which is the last 8-bits
xor edx, edx ; performing XOR P with itself ie. edx with edx that has P

and eax, 0ffffff00h ; purpose: to get all the values of the other register but take the value of last 8-bits from the other register
or eax, edx ; concanitating the eax and edx register bit values

push eax ; pushing eax to stack so that we can display
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits procedure so that we can see the bit pattern
add esp, 4 ; adding 4 to esp

mov part4_fpop, eax ; moving eax value to DWORD
put_str space ; for the display formatting
put_fp part4_fpop ; displaying the floating point value for the resultant
put_ch newline ; for the newline



; ||--#####PART FIVE#####--||

put_ch newline ; for the newline
put_str part5 ; displaying part 5
put_ch newline ; for the display

finit ; initializes the floating point processor, clearing the stack

fld val_one ; loads stack top ST with floating point data value
fld part1_fpop ; converts integer value to corresponding fp value that is pushed onto the stack
fld val_two ; loads stack top ST with floating point data value

fldpi ; pushes PI onto fp stack          
fld part3_fpop ; converts integer value to corresponding fp value that is pushed onto the stack

fdiv st, st(3) ; calculates ST / ST(3);
fxch ; exchange values in ST and ST(1)

fmulp st(4), st ; multiplies ST(4) and ST; replaces ST(4) by the product & pops ST from stack
fdiv st, st(3) ; calculates ST / ST(3);
fmulp st(1), st ; multiplies ST(1) and ST; replaces ST(1) by the product & pops ST from stack
fchs ; change sign:  ST = - ST

fdiv val_two ; pops ST and ST(1); calculates ST(1) / ST; pushes quotient onto the stack
fst part5_fpop ; copies stack top ST value to memory

finit ; initializes the floating point processor, clearing the stack
and part5_fpop, 0ffff0000h ; zerolize the rightmost two bytes

put_ch newline ; for newline
put_str space ; formatting as a tab to make display look better
put_fp part5_fpop ; displays float value in parameter
put_ch newline ; for a newline

; ||--######PART SIX######--||

put_ch newline ; for a newline
put_str part6 ; displays part 6
put_ch newline ; for a newline
put_ch newline ; for a newline

push part5_fpop ; pushing the value of the DWORD in to stack so that we can display it
put_str space ; formatting as a tab to make display look better
call display_bits ; calling display_bits proc to display the bits
add esp, 4 ; adding 4 to esp

; binary bits in DWORD "part5_fpop" to Scientific Notation
;finit ; initializes the floating point processor, clearing the stack
;fld part5_fpop ; loads stack top ST with floating point data value
;put_fp part5_fpop


   main_done:

        done ; display completion/do-again message
        cmp eax, 0 ; EAX = 0?
        je rerun_main ; yes, go run program again
        xor eax, eax ; exit with return code 0
        ret

_MainProc ENDP

;------------------------------------------------------------

                                            
display_bits PROC

.DATA
        my_mask    DWORD    ?
        space_char DWORD   32
      
.CODE      
        push ebp ; save base pointer
        mov ebp, esp ; establish stack frame

        push eax ; pushing eax
        push ebx ; pushing ebx
        push ecx ; pushing ecx
        push edx ; pushing edx

        mov ecx, 8 ; bit-separator counter
        push ecx ; save bit-separator counter
        mov ecx, 32 ; bit-shift counter
        mov my_mask, 80000000h ; initialize mask
        mov edx, DWORD PTR [ebp+8] ; get value of parameter

    display_bit:

        mov eax, edx ; moving edx into eax
        and eax, my_mask ; masking
        cmp eax, 0 ; eax = 0?
        je display_0 ; if true goto display
        mov ebx, 1 ; moving 1 into ebx
        put_i ebx ; displaying bit pattern in ebx
        jmp check_separate ; goto check_seperate:

    display_0:

        mov ebx, 0 ; moving 0 into ebx
        put_i ebx ; displaying bit-pattern in ebx

    check_separate:

        pop ebx ; poping ebx
        dec ebx ; decrementing ebx
        cmp ebx, 0 ; ebx != 0
        jne shift_mask ; if true goto shift_mask
        put_ch space_char ; putting space char
        mov ebx, 8 ; moving 8 in to ebx

    shift_mask:

        push ebx ; pushing ebx
        shr my_mask, 1 ; shifting right the mask by one position
        loop display_bit ; looping
        put_ch newline ; displaying newline
        put_ch newline ; displaying newline
        add esp, 4 ; adding 4 to esp
        pop edx ; poping edx
        pop ecx ; poping ecx
        pop ebx ; poping ebx
        pop eax ; poping eax
        pop ebp ; poping ebp
        ret

display_bits ENDP

proc_name PROC

            push ebp           ; save base pointer
            mov  ebp, esp      ; establish stack frame

            ;; push registers used by this proc

            ;; proc code inserted here

            ;; pop registers used by this proc

            pop  ebp           ; restore base pointer
            ret

proc_name ENDP

;----------------------------------------------------------------------

stat_flags MACRO

        fstsw ax        ; FPU status word to AX
        sahf            ; condition code bits to flags
ENDM

;----------------------------------------------------------------------

END  ; end of source code

Programming Assignment 4 - the concept of big endian and little endian!




; Title: CISS 360 Programming Assignment 4
;------------------------------------------------------------
; Description: This program interprets with the concept of big
; endian and little endian. Big Endian is used in UNIX system.
; This pentium assembly language program includes a PROC named
; "Endian" that takes DWORD parameter and reverses its bytes,
; returning the new value in EAX.
; The main PROC is invoked using the PROC for the Endian and a
; display PROC is also used to display the bits.
;------------------------------------------------------------
; Programmer: Prajwal Shrestha
; Professor: Ronald J. Hart
;------------------------------------------------------------
; Date/Time: Wednesday, February 17, 2011 10:20 PM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h  ; header file for console input/output
.STACK 4096   ; reserve 4096-byte (1024 DWORD) stack
;------------------------------------------------------------
;                   Input/Output Facility                  
;------------------------------------------------------------
; Macro     Operand
;
; put_ch    DWORD/Reg     (Displays character in parameter)
; get_ch    None          (Loads character from keyboard into EAX)
; put_str   BYTE          (Displays string beginning at given address)
; put_i     DWORD/Reg     (Displays signed integer value in parameter)
; put_u     DWORD/Reg     (Displays unsigned integer value in parameter)
; put_fp    REAL4/Reg     (Displays float value in parameter)
; get_i     DWORD         (Loads signed integer entered at keyboard into parameter)
; get_u     DWORD         (Loads unsigned integer entered at keyboard into parameter)
; get_fp    REAL4         (Loads float entered at keyboard into parameter)
; get_str   BYTE          (Loads string entered at keyboard into parameter)
;------------------------------------------------------------

.DATA   ;****************************************************
        ;*                  Data Section                    *
        ;****************************************************
prompt1 BYTE "Enter the number to convert or swap:  ", 0
prompt2 BYTE "Input Bit Pattern:  ", 0
prompt3 BYTE "Converted (Swaped):  ", 0
user_input DWORD ?
newline DWORD 10


.CODE   ;****************************************************
        ;*                  Code Section                    *
        ;****************************************************

_MainProc PROC

    rerun_main:

put_ch newline ; starts the newline for display puropses
put_str prompt1 ; displays the message to get the input from the user
get_i user_input ; DWORD user_input gets the input the user types
put_ch newline ; starts the newline for display puropses

mov eax, user_input ; moving user_input to the register eax
push eax ; 1st ELEMENT pushed: pushing eax to procedure
put_str prompt2 ; displays the message to user informing the input bit pattern is..
call display_bits ; calling display_bits procedure
add esp, 4 ; restoring the stack pointer back

push eax ; 1st ELEMENT pushed: pushing eax to procedure
call endian ; calling endian procedure
add esp, 4 ; restoring the stack pointer back

push eax ; 1st ELEMENT pushed: pushing eax to procedure
put_str prompt3 ; displays the message to user the endian bit pattern ie swapped bit pattern is..
call display_bits ; calling display_bits procedure
add esp, 4 ; restoring the stack pointer back

    main_done:

        done ; display completion/do-again message
        cmp         eax, 0 ; EAX = 0?
        je          rerun_main ; yes, go run program again
        xor         eax, eax ; exit with return code 0
        ret

_MainProc ENDP

;------------------------------------------------------------                                                                
                                                                    
                                                                    
                                            
display_bits PROC

.DATA
        my_mask    DWORD    ?
        space_char DWORD   32
      
.CODE      
        push ebp ; save base pointer
        mov ebp, esp ; establish stack frame

        push eax ; pushing eax
        push ebx ; pushing ebx
        push ecx ; pushing ecx
        push edx ; pushing edx

        mov ecx, 8 ; bit-separator counter
        push ecx ; save bit-separator counter
        mov ecx, 32 ; bit-shift counter
        mov my_mask, 80000000h ; initialize mask
        mov edx, DWORD PTR [ebp+8]      ; get value of parameter

    display_bit:

        mov eax, edx ; moving edx into eax
        and eax, my_mask ; masking
        cmp eax, 0 ; eax = 0?
        je display_0 ; if true goto display
        mov ebx, 1 ; moving 1 into ebx
        put_i ebx ; displaying bit pattern in ebx
        jmp check_separate ; goto check_seperate:

    display_0:

        mov ebx, 0 ; moving 0 into ebx
        put_i ebx ; displaying bit-pattern in ebx

    check_separate:

        pop ebx ; poping ebx
        dec ebx ; decrementing ebx
        cmp ebx, 0 ; ebx != 0
        jne shift_mask ; if true goto shift_mask
        put_ch space_char ; putting space char
        mov ebx, 8 ; moving 8 in to ebx

    shift_mask:

        push ebx ; pushing ebx
        shr my_mask, 1 ; shifting right the mask by one position
        loop display_bit ; looping
        put_ch newline ; displaying newline
        put_ch newline ; displaying newline
        add esp, 4 ; adding 4 to esp
        pop edx ; poping edx
        pop ecx ; poping ecx
        pop ebx ; poping ebx
        pop eax ; poping eax
        pop ebp ; poping ebp
        ret

display_bits ENDP


endian PROC

            push ebp ; save base pointer
            mov ebp, esp ; establish stack frame

            ;; push registers used by this proc
push ebx ; save ebx
push edx ; save edx

mov ebx, [ebp + 8] ; 1st pushed from main: the user input passed from main to ebx proc
mov eax, ebx ; moving value from ebx to eax preserve the value in ebx ie. user input

            ;; proc code inserted here
and eax, 000000ffh ; performing AND operation with last 8 bits of the eax and all zeros
sal eax, 24 ; shifting left so that it moves 24 position left which gives us the last 8 bits in to first
push eax ; pushing the bits value in eax

mov eax, ebx ; again getting the fresh user input to eax
and eax, 0000ff00h ; performing AND operation with second last 8-bits of the eax AND all zeros
sal eax, 8 ; shifting left 8 bits so that it moves 8 position left which would swap the bits pattern
push eax ; pushing eax to preserve


mov eax, ebx ; again getting the fresh user input to eax
and eax, 00ff0000h ; performing AND operation with second 8-bits of the eax AND all zeros
sar eax, 8 ; shifting right 8 bits so that it moves 8 position right which would swap the bits pattern
push eax ; pushing eax to preserve


mov eax, ebx ; again getting the fresh user input to eax
and eax, 0ff000000h ; performing AND operation with first 8-bits of the eax AND all zeros
sar eax, 24 ; shifting right 24 bits so that it moves 24 position right which would swap the bits pattern

mov edx, eax ; moving eax bit-pattern into edx

pop eax ; poping the eax bit pattern pushed last (latest)
or edx, eax ; somewhat like concanitating bit patterns stored in edx and eax

pop eax ; poping the eax bit pattern pushed last (latest)
or edx, eax ; again concanitating bit patterns stored in edx and eax using OR

pop eax ; poping the eax bit pattern pushed last (latest)
or edx, eax ; again concanitating bit patterns stored in edx and eax using OR

mov eax, edx ; moving edx which contains the swaped bit patters to eax


            ;; pop registers used by this proc

            pop ebx ; restore ebx
pop edx ; restore edx
pop ebp ; restore base pointer

            ret

endian ENDP

;----------------------------------------------------------------------

stat_flags MACRO

        fstsw ax ; FPU status word to AX
        sahf ; condition code bits to flags
ENDM

;----------------------------------------------------------------------

END  ; end of source code