0% found this document useful (0 votes)
5 views

Lab 4

Uploaded by

kingorijoseph180
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 4

Uploaded by

kingorijoseph180
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Example 1

 Task: Convert the character in AL to upper case.


o Solution: Use the AND instruction to clear bit 5.
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov eax,'a' ; AL = 01100001b


and eax,11011111b ; AL = 01000001b

INVOKE ExitProcess, 0
main ENDP
END main

Example 2

 Task: Convert a binary decimal byte into its equivalent ASCII decimal digit.
o Solution: Use the OR instruction to set bits 4 and 5
o The ASCII digit '6' = 00110110b
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov eax,6 ; AL = 00000110b


or eax,00110000b ; AL = 00110110b

INVOKE ExitProcess, 0
main ENDP
END main
Example 3

Task: Turn on the keyboard CapsLock key


• Solution: Use the OR instruction to set bit 6 in the keyboard flag byte at 0040:0017h in the BIOS data
area

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov eax,40h ; BIOS segment


mov esp,eax
mov ebx,17h ; keyboard flag byte
or BYTE PTR [ebx],01000000b ; CapsLock on

INVOKE ExitProcess, 0
main ENDP
END main

 This code only runs in Real-address mode, and it does not work under
Windows NT, 2000, or XP

Example 4

 Task: Jump to a label if an integer is even.

o Solution: AND the lowest bit with a 1. If the result is Zero, the number was even.
mov ax, wordVal
and ax, 1 ; low bit set?
jz EvenValue ; jump if zero flag set

Example 4

 Task: Jump to a label if the value in AL is not zero.


o Solution: OR the byte with itself, then use the JNZ (jump if not zero) instruction.
or a ,a
jnz IsNotZero ; jump if not zero

ORing any number with itself does not change its value.

Example 5 (CMP Instruction)

 Compares the destination operand to the source operand


o Nondestructive subtraction of source from destination (destination operand is not
changed)
o Syntax: CMP destination, source (OF,SF,ZF,CF,AF,PF)

 Example: destination == source (unsigned)


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov al,5
cmp al,5 ; Zero flag set

INVOKE ExitProcess, 0
main ENDP
END main

 Example: destination < source (unsigned)


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov al,4
cmp al,5 ; Carry flag set
INVOKE ExitProcess, 0
main ENDP
END main

 Example: destination > source (unsigned)


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov al,6
cmp al,5 ; ZF = 0, CF = 0

INVOKE ExitProcess, 0
main ENDP
END main

 Example: destination > source (signed)


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov al,5
cmp al,-2 ; Sign flag == Overflow flag

INVOKE ExitProcess, 0
main ENDP
END main

 Example: destination < source (signed)


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
; define your variables here
.code

main PROC
; write your assembly code here

mov al,-1
cmp al,5 ; Sign flag != Overflow flag

INVOKE ExitProcess, 0
main ENDP
END main

Example 6 Jump if above

Comparisons come in signed and unsigned flavors, because when you're comparing positive and
negative signed numbers, the sign bit counts for a negative value. On x86, you use the same "cmp"
instruction, but "jl" and "jg" (jump if Less, or Greater) do a signed comparison, while "ja" and "jb" (jump
if Above, or Below) do an unsigned comparison. For example, comparing the bit pattern X=all-zeros to
Y=all-ones, as an unsigned comparison, X<Y, because Y is really big; while as a signed comparison X>Y,
because Y is negative one.

What is the output?


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov eax, 1000h ; all-zeros


mov ecx, 2000h; all-ones
cmp eax, ecx
ja did_jump
mov eax, 3000h
ret

did_jump:
mov eax,4000h
ret

INVOKE ExitProcess, 0
main ENDP
END main

Example 7 Jump if greater


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here
.code

main PROC
; write your assembly code here

mov eax, 2000h ; all-zeros


mov ecx, 1000h; all-ones
cmp eax, ecx
jg did_jump
mov eax, 3000h
ret

did_jump:
mov eax,4000h
ret

INVOKE ExitProcess, 0
main ENDP
END main

Example 8 Jcond

Write code to demonstrate the other jump conditions as listed in the lecture slides.

Example 9

• Find the first even number in an array of unsigned integers (slide 34)

You might also like