Download presentation
Published byTobias Thomas Modified over 9 years ago
1
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
By Dr. Syed Noman
2
Conditional statement: Program that checks even or odd for digits between 0 to 9
jmp exit print_odd: mov dx, offset msg3 exit: call puts mov ax,4c00h int 21h puts: mov ah, 9h int 21h ; output string ret getc: mov ah, 1h end .model small .data msg1 db 13,10,’input a digit :: $’ msg2 db 13,10,’it is even :: $’ msg3 db 13,10, ’it is odd :: $’ .code mov mov ds,ax mov dx, offset msg1 call puts call getc mov dl,al and dl, b ; boolean stmt cmp dl,0 je print_even jmp print_odd print_even: mov dx, offset msg2
3
Implementing loop without loop statement
Printing ‘*’ ten times on the screen mov ah,2 mov dl,’*’ mov cx,10 disp_char: int 21h dec cx cmp cx,0 jne disp_char
4
General format for using loop instruction
mov cx, count ; count = # of times to repeat loop start_loop: ; use any label name <loop body> ; while cx > 0 ; repeat loop body instructions loop start_loop
5
Implementing loop with loop statement
mov ah,2 mov dl, ‘*’ ; al = ‘*’ mov cx, ; cx = 10 ; loop count disp_char: int 21h; display ‘*’ loop disp_char ; cx = cx - 1, if (cx != 0) Here, cx is initialised to 10, the number of iterations required. The instruction loop disp_char first decrements cx and then tests if cx is not equal to 0, branching to disp_char only if cx does not equal 0.
6
Loop Example with procedure
Example : Using loop instruction. mov al, ‘*’ ; al = ‘*’ mov cx, ; cx = 10 ; loop count disp_char: call putc ; display ‘*’ loop disp_char ; cx = cx - 1, if (cx != 0)
7
Program to print name 10 times
.model small .data myName db 13,10,‘Dr. Syed Noman$’ .code mov mov ds,ax mov cx,10 mov ah,9 mov dx, offset myName again: int 21h loop again mov ax,4c00h end
8
Program to print name 10 times (with procedure)
.model small .data myName db 13,10,‘Dr. Syed Noman$’ .code mov mov ds,ax mov cx,10 mov dx, offset myName again: call puts loop again mov ax,4c00h int 21h puts: mov ah, 9h int 21h ; output string ret end
9
Program print digits from 0 to 9
.model small .code mov cx,10 mov ah,2 mov dl,30h; mov dl, ‘0’; mov dl,48; mov dl, b again: int 21h inc dl loop again mov ax,4c00h end
10
Program print digits from 0 to 9 (with procedure)
.model small .code mov cx,10 mov dl,30h again: call putc inc dl loop again mov ax,4c00h int 21h putc: mov ah, 2h ret end
11
Program print digits from 0 to 9,with procedure
Program print digits from 0 to 9,with procedure. Use appropriate message) .model small .data msg db 13,10, ‘Decimal Digits:’, 13,10, ‘$’ .code mov mov ds,ax mov dx, offset msg call puts mov cx,10 mov dl,30h again: call putc inc dl loop again mov ax,4c00h int 21h puts: mov ah, 9h int 21h ; output string ret putc: mov ah, 2h end
12
Program print even numbers from 0 to 9
.model small .code mov cx,5 mov ah,2 mov dl,30h again: int 21h add dl,2 loop again mov ax,4c00h end
13
Program print even numbers from 0 to 9 (with procedure)
.model small .code mov cx,5 mov dl,30h again: call putc add dl,2 loop again mov ax,4c00h int 21h putc: mov ah, 2h ret end
14
Nested loops .data count dd ? .code mov cx,value ; set outer loop count L1: mov count, cx ; save outer loop count mov cx,New_value ; set inner loop count L2: ;(inner loop code) loop L2 ; repeat the inner loop mov cx,count ; restore outer loop count loop L1 ; repeat the outer loop
15
Practice Program in Class
Write an assembly code that prints the numbers from 1 to 4, 6 times on the screen. Each sequence of numbers from 1 to 4 are separated by new line. Output:
16
DUP Operator Use DUP to allocate (create space for) an array or string. Syntax: Counter DUP ( argument ) Counter and argument must be constants or constant expressions.
17
DUP Operator … .model small .data
msg db ‘Printing Alphabets count corresponding to position:’, 13,10 db “a”,13,10, db “bb”,13,10 db “ccc”,13,10 db “dddd”,13,10,’$’ msg2 db ‘Printing Alphabets count corresponding to position:’, 13,10 db 1 dup (‘a’) db 2 dup (‘b’) db 3 dup (‘c’) db 4 dup (‘d’) .code mov mov ds,ax …
18
Assignment#2e, 2f, 2g Write program with procedures, message to print numbers from 9 to 0. Ask the count from the user to print his name. minimum zero, maximum 9. (Hint: put the value in cl, make ch zero) Write program with procedure and message, to print odd numbers between 9 to 0 inclusive.
19
Assignment#2h Print the following output on the command window using the DUP Operator: ******** ****** **** **
20
Assignment#2i Write an assembly code that prints the following on the console. 1 1 2 1 2 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.