Lab 4
Lab 4
.data
; define your variables here
.code
main PROC
; write your assembly code here
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
INVOKE ExitProcess, 0
main ENDP
END main
Example 3
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
; define your variables here
.code
main PROC
; write your assembly code here
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
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
ORing any number with itself does not change its value.
.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
.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
.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
.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
main PROC
; write your assembly code here
mov al,-1
cmp al,5 ; Sign flag != Overflow flag
INVOKE ExitProcess, 0
main ENDP
END main
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.
.data
; define your variables here
.code
main PROC
; write your assembly code here
did_jump:
mov eax,4000h
ret
INVOKE ExitProcess, 0
main ENDP
END main
.data
; define your variables here
.code
main PROC
; write your assembly code here
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)