Showing posts with label #Assembly(code). Show all posts
Showing posts with label #Assembly(code). Show all posts

Sunday, January 16, 2022

Write a program that compute v5 = v1 + v2/8 + v3*5 (v1, v2, and v3 are unsigned byte variables).

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA

   val1 dword 15

   val2 dword 34

   val3 dword 77

   str1 byte "V5 is :  ",0                                        

 

.CODE

MAIN PROC

mov edx,0

mov eax,val2

mov ebx,98

div ebx

 

mov ebx,val1

add eax,ebx

mov ebx,eax

mov eax,val3

mov ecx,5

mul ecx

add eax,ebx

 

mov edx,OFFSET str1

call WriteString

call WriteInt

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write a program that find v4 = v1/2 + v2 *16 (only use only shift instructions).

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA

   val1 word 5

   val2 word 7

   val3 word ?  

   str1 byte "v4 is :  ",0                                        

 

.CODE

MAIN PROC

mov eax,0

mov ax,val2

shl ax,9

mov ebx,0

mov bl,al

mov ax,val1

shr ax,8

add ax,bx

mov val3,ax

mov edx,OFFSET str1

call WriteString

call WriteInt

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write a program that counts number of 1 bits (bits having value 1) stored in register AX. Display whether there are even or odd number of 1 bits.

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD                                          

.CODE

MAIN PROC

    call ReadInt

AND eax,1

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write a program that will prompt the user to enter a hex digit character ("0" "9" or "A" "F"), display it on the next line in decimal.

 Include irvine32.inc

;AddTwo.asm-adds two 32-bit integer.

;chapter3 example

.386

.stack 4096

ExitProcess proto,dwExitCode:dword

.data

str1 byte "Please Enter The HexaDecimal Value:",0

str2 byte "Decimal value is:",0

str3 byte "Do you start again:",0

str4 byte "wrong choice",0

pr byte 'y',0

choice dword ?

.code

main proc

start:

mov edx,offset str1

call WriteString

call ReadHex

mov edx,offset str2

call WriteString

call WriteDec

call crlf

mov edx,offset str3

call WriteString

call ReadChar

mov choice,eax

cmp choice,'y'

je print

jne next

print:

jmp start

next:

call crlf

mov edx,offset str4

call WriteString

 

  invoke ExitProcess,0

  main endp

  end main

Write assembly language code to reverse a string “abcdef” using stack.

 Include irvine32.inc

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.data

rev byte "abcdef",0

ns  DWORD ?

str byte "After reversing string:   ",0

.code

main proc

    mov edx, OFFSET rev

    mov Ecx, SIZEOF rev

    mov ns,ecx

    mov  esi,0

L1: movzx eax,rev[esi]  

    push eax               

    inc esi

    loop L1

    mov  ecx,ns

    mov  esi,0

L2: pop  eax      

    mov  rev[esi],al     

    inc  esi

    loop L2

  mov EDX ,OFFSET str

    call WriteString

    mov EDX ,OFFSET rev

    call WriteString         

 

INVOKE EXITPROCESS,0

main ENDP

END main

Write assembly language code to swap two numbers using stack .

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA                                                

val1 dword 32                                              

val2 dword 45

str1 BYTE "Before swapping :      ",0

str2 BYTE "Before swapping :       ",0

x BYTE "   After Swaping :",0

.CODE

MAIN PROC

mov edx,OFFSET str1

call WriteString

mov eax,val1

call WriteInt

mov edx,OFFSET x

call WriteString

mov eax,val2

call WriteInt

call crlf

mov edx,OFFSET str2

call WriteString

mov eax,val1

push eax

mov ebx,val2

push ebx

pop eax

call WriteInt

mov edx,OFFSET x

call WriteString

pop eax

mov eax,eax

call WriteInt

call crlf

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write instructions that and pop the largest number of array, after pop again print to array.

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA                                              

array dword 6,6,78,1,3,56,54,53,85,100

max dword ?

str1 BYTE "Largest Number is :  ",0

.CODE

MAIN PROC

mov edx,OFFSET array

mov ecx,LENGTHOF array

 mov esi,0

l1:

mov eax,array[esi]

push eax

add esi,4

loop l1

mov edx,OFFSET array

mov ecx,LENGTHOF array

 mov esi,0

L2:

pop eax

mov max,eax

l3::

 cmp ecx,0

 je last

 cmp eax,array[esi]

 ja again

 mov eax,array[esi]

 add esi ,4

 loop l3

 jmp last

 again:

 add esi,4

 dec ecx

 jmp l3

mov array[esi],eax

add esi,4

loop l2

 last:

 mov max,eax

 mov edx,OFFSET str1

 call WriteString

 call WriteInt

 call crlf

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write assembly language instructions to pop the 3 numbers.

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA                                                  

array DWORD 34,54,23

array2 dword 3 DUP(0)

.CODE

MAIN PROC                                               

MOV EDX,OFFSET array

mov esi,0

mov ecx,3

l1:

mov eax,array[esi]

push eax

add esi,4

loop l1

MOV EDX,OFFSET array2

mov esi,0

mov ecx,3

L2:

pop eax

mov array2[esi],eax

add esi,4

loop l2

MOV EDX,OFFSET array2

mov esi,0

mov ecx,3

L3:

mov eax,array2[esi]

call WriteInt

call crlf

add esi,4

loop l3

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Write assembly language instructions to push 10 numbers in stack.

 INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA                                      

array DWORD 23,45,65,3,456,7,43,,67,5,34

.CODE

MAIN PROC

MOV EDX,OFFSET array

mov esi,0

mov ecx,10

l1:

mov eax,array[esi]

push eax

add esi,4

loop l1

INVOKE EXITPROCESS,0

MAIN ENDP

END MAIN

Use procedure to swap two numbers.

INCLUDE C:\IRVINE\IRVINE32.INC

.386

.STACK 4096

EXITPROCESS PROTO,DWEXITCODE:DWORD

.DATA                                             

tmp dword ?

str1 BYTE "Enter  value of A here :  ",0

str2 BYTE "Enter value of b here :  ",0

str3 byte "after swaping value of A :",0

str4 byte "after swaping value of b :",0

a dword ?

b dword ?

.CODE

MAIN PROC

call swap

INVOKE EXITPROCESS,0

MAIN ENDP

swap proc

mov edx,offset str1

call WriteString

call ReadInt

mov a,eax

mov edx,offset str2

call WriteString

call ReadInt

mov b,eax

mov ecx,a

mov ebx,b

mov tmp,ecx

mov eax,0

mov eax,ebx

mov edx,offset str3

call WriteString

call WriteInt

mov eax,0

mov ebx,tmp

mov eax,ebx

call crlf

mov edx,offset str4

call WriteString

call WriteInt

 ret

swap endp

END MAIN