Sunday, April 10, 2011

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



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.