Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Examples of what we have learned so far

Example
• Write a program that adds 5 bytes of data and save the result. The
data should be following hex numbers: 25,12,15,1F and 2B
DIY
• Write a program that total 5 bytes of data. Each byte represents the
daily wages of a worker. This person does not make more than $255
(0FFH) a day. The decimal data is as follows: 125, 235, 197, 91 and 48
Example
• Write a program that adds 4 words of data and save the result. The
values are: 234DH, 1DE6H, 3BC7H and 566AH
DIY
• Write a program that adds that adds: 27345, 28521, 29533, 30105,
32375
Example
• Assume there is a class of five people with the following grades: 69,
87, 96, 45 and 75. Find the highest grade.
Example
• Write a program that finds the number of 1s in a byte.
Kind of questions you can expect
• Find even numbers from given set of numbers
• Find odd numbers from given set of numbers
• Find prime numbers from given set of numbers
• Find numbers multiple of 3
• Find the average (mean) of the given set of numbers
• Find No. of zeroes in the given byte/word
• Solve equations containing (+-*/) e.g. Y=mx+c
Write a program that adds 5 bytes of data and save the result. The data should be
following hex numbers: 25,12,15,1F and 2B
.MODEL SMALL
.STACK 64
.DATA
DATA_IN DB 25H, 12H, 15H, 1FH, 2BH
SUM DB ?
.CODE
.STARTUP
MOV CX,05
LEA BX,DATA_IN
MOV AL,0
AGAIN: ADD AL,[BX]
INC BX
DEC CX
JNZ AGAIN
MOV SUM,AL
.EXIT
END
Write a program that adds 4 words of data and save the result. The values are:
234DH, 1DE6H, 3BC7H and 566AH
.MODEL SMALL
.STACK 64
.DATA
DATA_IN DW 234DH, 1DE6H, 3BC7H, 566AH
SUM DW ?
.CODE
.STARTUP
MOV CX,04
LEA DI,DATA_IN
MOV BX,00
ADD_LP: ADD BX,[DI]
INC DI
INC DI
DEC CX
JNZ ADD_LP
MOV SUM,BX
.EXIT
END
Write a program that transfers 6 bytes of data from memory locations with offset of
0010H to memory locations with offset of 0028H.
.MODEL SMALL
.STACK 64
.DATA
ORG 0010H
DATA_IN DB 23H, 1DH, 3BH, 56H, 2BH, 0C4H
ORG 0028H
COPY DB 6 DUP(?)
.CODE
.STARTUP
MOV CX,06
MOV DI, OFFSET COPY
LEA SI,DATA_IN
MOV_LP: MOV AL,[SI]
MOV [DI],AL
INC SI
INC DI
DEC CX
JNZ MOV_LP
.EXIT
END
Write a program that total 5 bytes of data. Each byte represents the daily wages of a worker. This person
does not make more than $255 (0FFH) a day. The decimal data is as follows: 125, 235, 197, 91 and 48
.MODEL SMALL
.STACK 64
.DATA
COUNT EQU 5
DATA_IN DB 125, 235, 197, 91, 48
SUM DW ?
.CODE
.STARTUP
MOV CX,COUNT
LEA SI,DATA_IN
MOV AX,00
BACK: ADD AL,[SI]
JNC OVER
INC AH
OVER: INC SI
DEC CX
JNZ BACK
MOV SUM,AL
.EXIT
END
Adds: 27345, 28521, 29533, 30105, 32375
.MODEL SMALL
.STACK 64
.DATA
DATA_IN DW 27345, 28521, 29533, 30105, 32375
SUM DW 2 DUP(?)
.CODE
.STARTUP
MOV CX,05
LEA SI,DATA_IN
MOV AX,00 ;To hold sum
SUB BX,BX ;To hold carry
BACK : ADD AX,[SI]
ADC BX,0
INC SI
INC SI
DEC CX
JNZ BACK
MOV SUM,AX
MOV SUM+2,BX
.EXIT
END
Assume there is a class of five people with the following grades: 69, 87, 96, 45 and
75. Find the highest grade.
.MODEL SMALL
.STACK 64
.DATA
GRADES DB 69, 87, 96, 45, 75
HIGHEST DB ?
.CODE
.STARTUP
MOV CX,05
LEA BX, GRADES
SUB AL,AL
AGAIN: CMPAL,[BX]
JG NEXT ;Jump if AL have the highest value
MOV AL,[BX] ;else AL holds new highest value
NEXT: INC BX
LOOP AGAIN
MOV HIGHEST,AL
.EXIT
END
Write a program that finds the number of 1s in a byte.
.MODEL SMALL
.STACK 64
.DATA
DATA1 DB 10010111B
COUNT DB ?
.CODE
.STARTUP
SUB AL,AL
MOV DL,8
MOV AL,DATA1
AGAIN: ROL AL,1
JNC NEXT ;Check for 1’s
INC BL ;If CF=1, then add 1 to BL
NEXT: DEC DL
JNZ AGAIN
MOV COUNT,BL
.EXIT
END
Multiply using SHIFT and ADD instructions

Multiply AX by 10 (1010) => (10AX = 2AX + 8AX)


.MODEL TINY
.CODE
.STARTUP
SHL AX,1 ;AX times 2
MOV BX,AX ;Save 2AX
SHL AX,2 ;AX times 8
ADD AX,BX ;ADD 2AX + 8AX => 10AX
.EXIT
END

Multiply AX by 18 (10010) => (18AX = 2AX + 16AX)


.MODEL TINY
.CODE
.STARTUP
SHL AX,1 ;AX times 2
MOV BX,AX ;Save 2AX
SHL AX,3 ;AX times 16
ADD AX,BX ;ADD 2AX + 16AX => 18AX
.EXIT
END

Multiply AX by 5 (101) => (5AX = 1AX + 4AX)


.MODEL TINY
.CODE
.STARTU
MOV BX,AX ;Save 1AX
SHL AX,2 ;AX times 4
ADD AX,BX ;ADD 1AX + 4AX => 5AX
.EXIT
END

You might also like