Monday, August 2, 2010

Example of C++ Do While

Codes:

#include
#include
void main()
{
Clrscr();
int i = 0;
do
{
printf("%d",i++);
} while (i < 3);
}

Output:
1 2 3

C++ Do while Statement

The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time. The format of the do-while loop is shown in the box at the right.
The test condition must be enclosed in parentheses and FOLLOWED BY A SEMI-COLON. Semi-colons also follow each of the statements within the block. The body of the loop (the block of code) is enclosed in braces and indented for readability. (The braces are not required if only ONE statement is used in the body of the loop.)
The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.

Syntax:

do
{
Statment;
}while(condition);

Example of C++ For Loop

Counting from 1 to 5

Codes:
#include
#include

void main ()
{
clrscr();

int x,y=0; <-- I assign the value 0 to y to initialize.

For(x=1;x<=5;x++) <-- This funtion is the for loop. It varies or counts the loop 1-5.
{
y = y++;
Printf("y");
}
getch();
}

Output:
1 2 3 4 5

C++ For Loop Statement

This C++ for loop tutorial is written for beginning C++ students without previous C++ programming experience. There are C++ questions about the for loop inside the tutorial, for the students to answer and many code examples and explanations about the conditions of a C++ for loop. Some of the for loop examples have an if/else structure within the for loop structure.
 
The format of a for loop is:

for(Initialization;Condition;Status)
{
Statement/Statments;
}

Hello World C++

This program is most basic part of the C++. It enables you to Print out a certain word of "Hello World".

Codes:
#include <--- Directory of the TCPLUS
#include <--- Directory of the TCPLIB

Void Main ()
{ <-- Start of the Program
Clrscr(); <-- This function clears everything printed out.

Printf("Hello World"); <-- Printf functions that what will you print on the screen.
Remmember that ; functions stops the program.
getch();
} <-- End of the Program

After writing this program. Checking the program if theres and error is pressing Ctrl + F9.

#include
#include
Void Main ()
{
Clrscr();

Printf("Hello World");
getch();
}

Output:
Hello World

What is a computer program?

A computer program is a set of instructions that a programmer writes to tell a
computer how to carry out a certain task. The instructions, however, must be in a
language that the computer understands. Computers only understand binary language
i.e. that composed of 1’s and 0’s. This is a low level language and very hard to
program in. So humans invented higher level languages such as C++ or Pascal to
make the job easier. As you will see, these languages are nearly like English but you
don’t have the freedom to write what you like – there are still rules you have to
follow.
To convert a program in C++ to a binary or executable file that the computer can
understand we use a compiler. The compiler which I recommend using for this guide
is Borland’s command line compiler.

Introduction to C++

Welcome to my beginners guide to C++. If you are starting to program for the
first time, I hope that you find the chapters I have written useful. C++ is an excellent
language to start programming in – a lot of applications that you use are probably
written in c++ and once you learn some basic concepts, learning other languages, like
java for example, will be much easier.
There are 8 chapters altogether, including this one. I have kept the chapters short and
concise so you won’t get bored or weighed down by too much information. After each
chapter you can rearrange the code in the examples provided or make up your own
code. Programming is very much a practical subject so you will learn a lot by messing
about with code or even looking at other people’s code.

Fibonnacci Series

Codes:


mov ax,'00' ; initialize to all ASCII zeroes
mov di,counter ; including the counter
mov cx,digits+cntDigits/2 ; two bytes at a time
cld ; initialize from low to high memory
rep stosw ; write the data
inc ax ; make sure ASCII zero is in al
mov [num1 + digits - 1],al ; last digit is one
mov [num2 + digits - 1],al ;
mov [counter + cntDigits - 1],al

jmp .bottom ; done with initialization, so begin

.top
; add num1 to num2
mov di,num1+digits-1
mov si,num2+digits-1
mov cx,digits ;
call AddNumbers ; num2 += num1
mov bp,num2 ;
call PrintLine ;
dec dword [term] ; decrement loop counter
jz .done ;

; add num2 to num1
mov di,num2+digits-1
mov si,num1+digits-1
mov cx,digits ;
call AddNumbers ; num1 += num2
.bottom
mov bp,num1 ;
call PrintLine ;
dec dword [term] ; decrement loop counter
jnz .top ;
.done
call CRLF ; finish off with CRLF
mov ax,4c00h ; terminate
int 21h ;

Output:

Fibonacci(1): 1
Fibonacci(2): 1
Fibonacci(3): 2
Fibonacci(4): 3

Alarm

Codes:

cseg segment para public 'code'
org 100h
alarm proc far

; Memory-resident program to intercept the timer interrupt and display the
; system time in the upper right-hand corner of the display.
; This program is run as 'ALARM hh:mm x', where hh:mm is the alarm time and
; x is '-' to turn the display off. Any other value of x or no value will
; turn the clock on

intaddr equ 1ch*4 ; interrupt address
segaddr equ 62h*4 ; segment address of first copy
mfactor equ 17478 ; minute conversion factor * 16
whozat equ 1234h ; signature
color equ 14h ; color attribute

assume cs:cseg,ds:cseg,ss:nothing,es:nothing
jmp p150 ; start-up code

jumpval dd 0 ; address of prior interrupt
signature dw whozat ; program signature
state db 0 ; '-' = off, all else = on
wait dw 18 ; wait time - 1 second or 18 ticks
hour dw 0 ; hour of the day
atime dw 0ffffh ; minutes past midnite for alarm
acount dw 0 ; alarm beep counter - number of seconds (5)
atone db 5 ; alarm tone - may be from 1 to 255 - the
; higher the number, the lower the frequency
aleng dw 8080h ; alarm length (loop count) may be from 1-FFFF

dhours dw 0 ; display hours
db ':'
dmins dw 0 ; display minutes
db ':'
dsecs dw 0 ; display seconds
db '-'
ampm db 0 ; 'A' or 'P' for am or pm
db 'm'

tstack db 16 dup('stack ') ; temporary stack
estack db 0 ; end of stack
holdsp dw 0 ; original sp
holdss dw 0 ; original ss

p000: ; interrupt code
push ax ; save registers
push ds
pushf

push cs
pop ds ; make ds=cs
mov ax,wait ; check wait time
dec ax ; zero?
jz p010 ; yes - 1 second has elapsed
mov wait,ax ; not this time
jmp p080 ; return

p010: cli ; disable interrupts
mov ax,ss ; save stack
mov holdss,ax
mov holdsp,sp
mov ax,ds
mov ss,ax ; point to internal stack
mov sp,offset estack
sti ; allow interrupts

push bx ; save other registers
push cx
push dx
push es
push si
push di
push bp

mov ax,18 ; reset wait time
mov wait,ax

mov al,state ; are we disabled?
cmp al,'-'
jnz p015 ; no
jmp p070

p015: mov ah,0 ; read time
int 1ah ; get time of day
mov ax,dx ; low part
mov dx,cx ; high part
mov cl,4
shl dx,cl ; multiply by 16
mov bx,ax
mov cl,12
shr bx,cl ; isolate top 4 bits of ax
add dx,bx ; now in upper
mov cl,4
shl ax,cl ; multiply by 16
mov bx,mfactor ; compute minutes
div bx ; minutes in ax, remainder in dx
cmp ax,atime ; time to sound the alarm?
jnz p020 ; no
call p100 ; yes - beep the speaker twice
push ax
mov ax,acount ; get beep count
dec ax ; down by 1
mov acount,ax ; save beep count
cmp ax,0 ; is it zero?
jnz p018 ; no - keep alarm on
mov ax,0ffffh ; turn off alarm
mov atime,ax
p018: pop ax

p020: mov dsecs,dx ; save remainder
mov bx,60 ; compute hours
xor dx,dx ; zero it
div bx ; hours in ax, minutes in dx
mov dmins,dx ; save minutes

cmp ax,0 ; midnight?
jnz p030 ; no
mov ax,12 ; yes
jmp p040a ; set am

p030: cmp ax,12 ; before noon?
jb p040a ; yes - set am
jz p040p ; noon - set pm
sub ax,12 ; convert the rest
p040p: mov bl,'p'
jmp p040x

p040a: mov bl,'a'

p040x: mov ampm,bl
aam ; fix up hour
cmp ax,hour ; top of the hour?
jz p060 ; no

mov hour,ax
call p120 ; beep the speaker once

p060: add ax,3030h ; convert hours to ascii
xchg ah,al
mov dhours,ax

mov ax,dmins ; get minutes
aam
add ax,3030h ; convert to ascii
xchg ah,al
mov dmins,ax

mov ax,dsecs ; get seconds (remainder)
xor dx,dx
mov bx,60
mul bx
mov bx,mfactor
div bx ; seconds in ax
aam
add ax,3030h
xchg ah,al
mov dsecs,ax

xor ax,ax ; check monitor type
mov es,ax
mov ax,es:[410h] ; get config byte
and al,30h ; isolate monitor type
cmp al,30h ; color?
mov ax,0b000h ; assume mono
jz p061 ; its mono

mov ax,0b800h ; color screen address

p061: mov dx,es:[463h] ; point to 6845 base port
add dx,6 ; point to status port

mov es,ax ; point to monitor
mov bh,color ; color in bh
mov si,offset dhours ; point to time
mov di,138 ; row 1, col 69
cld
mov cx,11 ; loop count

p062: mov bl,[si] ; get next character

p063: in al,dx ; get crt status
test al,1 ; is it low?
jnz p063 ; no - wait
cli ; no interrupts

p064: in al,dx ; get crt status
test al,1 ; is it high?
jz p064 ; no - wait

mov ax,bx ; move color & character
stosw ; move color & character again
sti ; interrupts back on
inc si ; point to next character
loop p062 ; done?

p070: pop bp ; restore registers
pop di
pop si
pop es
pop dx
pop cx
pop bx
cli ; no interrupts
mov ax,holdss
mov ss,ax
mov sp,holdsp
sti ; allow interrupts

p080: popf
pop ds
pop ax
jmp cs:[jumpval]

p100 proc near ; beep the speaker twice
call p120
push cx
mov cx,20000
p105: loop p105 ; wait around
pop cx
call p120
push cx
mov cx,20000
p106: loop p106 ; wait around
pop cx
call p120
ret
p100 endp

p120 proc near ; beep the speaker once
push ax
push cx
mov al,182
out 43h,al ; setup for sound
mov al,0
out 42h,al ; low part
mov al,atone ; get alarm tone
out 42h,al ; high part
in al,61h
push ax ; save port value
or al,3
out 61h,al ; turn speaker on
mov cx,aleng ; get loop count
p125: loop p125 ; wait around
pop ax ; restore original port value
out 61h,al ; turn speaker off
pop cx
pop ax
ret
p120 endp

p150: ; start of transient code
mov dx,offset copyr
call p220 ; print copyright
mov ax,0
mov es,ax ; segment 0
mov di,segaddr+2 ; this program's prior location
mov ax,es:[di] ; get prior code segment
mov es,ax ; point to prior program segment
mov di,offset signature
mov cx,es:[di] ; is it this program?
cmp cx,whozat
jnz p160 ; no - install it
call p200 ; set state & alarm
int 20h ; terminate

p160: mov di,segaddr+2 ; point to int 62h
mov ax,0
mov es,ax ; segment 0
mov ax,ds ; get current ds
mov es:[di],ax ; set int 62h
mov si,offset jumpval
mov di,intaddr ; point to timer interrupt
mov bx,es:[di] ; get timer ip
mov ax,es:[di+2] ; and cs
mov [si],bx ; save prior ip
mov [si+2],ax ; and cs
mov bx,offset p000
mov ax,ds
cli ; clear interrupts
mov es:[di],bx ; set new timer interrupt
mov es:[di+2],ax
sti ; set interrupts
push ds
pop es
call p200 ; set state & alarm
mov dx,offset p150 ; last byte of resident portion
inc dx
int 27h ; terminate

p200 proc near ; set state & alarm
mov si,80h ; point to command line
mov ax,0
mov di,0ffffh ; init hours
mov bh,0
mov ch,0
mov dh,0 ; : counter
mov es:[state],bh ; turn clock on
mov cl,[si] ; get length
jcxz p210 ; it's zero

p203: inc si ; point to next char
mov bl,[si] ; get it
cmp bl,'-' ; is it a minus?
jnz p204 ; no
mov es:[state],bl ; turn clock off
push dx
mov dx,offset msg3 ; print msg
call p220
pop dx
jmp p206

p204: cmp dh,2 ; seen 2nd colon?
jz p206 ; yes - ignore seconds
cmp bl,':' ; colon?
jnz p205 ; no
inc dh
cmp dh,2 ; second colon?
jz p206 ; yes - ignore seconds
push cx
push dx
mov cx,60
mul cx ; multiply current ax by 60
pop dx
pop cx
mov di,ax ; save hours
mov ax,0
jmp p206
p205: cmp bl,'0'
jb p206 ; too low
cmp bl,'9'
ja p206 ; too high - can be a problem
sub bl,'0' ; convert it to binary
push cx
push dx
mov cx,10
mul cx ; multiply current value by 10
add ax,bx ; and add latest digit
pop dx
pop cx
p206: loop p203 ; done yet?
cmp di,0ffffh ; any time to set?
jz p210 ; no
add ax,di ; add hours
cmp ax,24*60
jb p209 ; ok
mov dx,offset msg1 ; print error message
call p220
jmp p210

p209: mov es:[atime],ax ; save minutes past midnight
mov ax,5
mov es:[acount],ax ; set alarm count
mov dx,offset msg2 ; print set msg
call p220
p210: ret
p200 endp

p220 proc near ; print message
push ax
mov ah,9
int 21h
pop ax
ret
p220 endp

copyr db 'Alarm - Clock',10,13,'$'
msg1 db 'Invalid time - must be from 00:00 to 23:59',10,13,'$'
msg2 db 'Resetting alarm time',10,13,'$'
msg3 db 'Turning clock display off',10,13,'$'

alarm endp
cseg ends
end alarm

This is a program w/c involves a little bit longer and harder codes.

Diagonal Letters

Output:



Codes:

.model small
.stack 50
.data
.code
main proc
mov ah, 02
mov dl, 61h
int 21h
start: sub dl, 20h
int 21h
push dx
mov dl, 0AH
int 21h
pop dx
add dl, 20h
inc dl
cmp dl, 5bh
jne start
int 20h
main endp
end main

Addition, Subtraction , Multiplication and Division (more than one digit , no decimals)

Sample Output:



Code:


msga db “Enter no 1: $” msgb db 13,10,”Enter no 2: $” msgc db 13,10,”Enter operation[A,S,M,D]: $” msgd db 13,10,”Result: $” msge db “-$”

ends

stack segment dw 128 dup(0)ends

code segmentstart:

mov ax, data mov ds, ax mov es, ax

lea dx, msga mov ah, 9 int 21h

mov ah,1 int 21h

sub al,30h push ax

lea dx, msgb mov ah, 9 int 21h

mov ah,1 int 21h

sub al,30h push ax

lea dx, msgc mov ah, 9 int 21h

mov ah,1 int 21h

mov cl,al

lea dx, msgd mov ah, 9 int 21h

pop

bx

pop ax

cmp cl,’A’ je a jne s1

s1:cmp cl,’S’ je s jne m1

m1:cmp cl,’M’ je m jne d1

d1:cmp cl,’D’ je d jne z

a: mov ah,0 add al,bl call print_ax

jmp z

s: mov ah,0 cmp al,bl ja sa jb sb

sa: sub al,bl jmp sc

sb: sub bl,al

lea dx,msge mov ah,09h int 21h

mov al,bl mov ah,0

sc: call print_ax

jmp z

m: mov ah,0 mov bh,0 mul bx call print_ax

jmp z

d: mov ah,al mov bh,bl mov dl,0 div bx call print_ax

z: mov ax, 4c00h int 21h

ends

print_ax proccmp ax, 0jne print_ax_r push ax

mov al, ‘0′

mov ah, 0eh int 10h pop ax ret print_ax_r: pusha mov dx, 0 cmp ax, 0 je pn_done mov bx, 10 div bx call print_ax_r mov ax, dx add al, 30h mov ah, 0eh int 10h jmp pn_donepn_done: popa ret endp

Conditional

``If'' statements are also done by using JMP instructions. Multiple conditions can be tested by using several JMP instructions. Here are some examples of how to do ``if'' statements:

Pascal:

IF (x = 10)
THEN
BEGIN
... (* then part 1 *)
END
ELSE
BEGIN
... (* else part 1 *)
END;

IF ((x<>10) AND (y>20))
THEN
BEGIN
... (* then part 2 *)
END;
C++:

if (x == 10) {
... // then part 1
}
else {
... // else part 1
}

if ((x!=10) && (y>20)) {
... // then part 2
}
Assembly:

LOAD r1, X ; load the variable x into r1
SET r2, 10 ; set r2 to immediate value 10
CMP r1, r2
JMP NEQ, ELSE1 ; if r1 <> r2, then jump to the else part, otherwise do the then part
... ; then part 1
JUMP END1 ; jump over the else part
ELSE1:
... ; else part 1
END1:

LOAD r1, X ; load the variable x into r1
SET r2, 10 ; set r2 to immediate value 10
CMP r1, r2
JMP NEQ, TEST2 ; if x <> 10 go on to next test
JUMP END2 ; otherwise skip past then part 2
TEST2:
LOAD r1, Y ; load the variable y into r1
SET r2, 20 ; set r2 to immediate value 20
CMP r1, r2
JMP GT, THEN2 ; if y > 20 go on to then part 2
JUMP END2 ; otherwise skip past
THEN2:
... ; then part 2
END2:

Loops

A loop is accomplished by using a JMP instruction. If the loop is to execute again, then you JMP back to the top of the loop body. If you are looping a certain number of times, it is usually easier to count down instead of count up, because it doesn't take an extra register to hold your final state. Here are examples of two different kinds of loops:

Pascal:

FOR i := 10 DOWNTO 1 DO
BEGIN
... (* loop1 body *)
END;
WHILE (x <> 20) DO
BEGIN
... (* loop2 body *)
END;
C++:

for (i=10; i > 0; i--) {
... // loop1 body
}
while (x != 20) {
... // loop2 body
}
Assembly:

SET r1, 10 ; r1 will take the place of i
SET r2, 1 ; r2 will hold the value to subtract each time
LOOP1TOP:
... ; loop1 body
SUB r1, r1, r2 ; subtract one from r1
CMP r1, r0
JMP NEQ, LOOP1TOP ; keep going until r1 gets to zero

SET r2, 20 ; r2 will hold our end condition
LOOP2TOP:
LOAD r1, X ; load the x variable
CMP r1, r2
JMP EQ, LOOP2END ; if we're done, skip to end of loop
... ; loop2 body
LOOP2END:

Usually in Assembly, we always use this kind of loops because it is helpful for the conditional statement.

Variables

When using assembly language, you will store most of your variables in registers. There are only 14 general purpose registers, however, so sometimes you will need to use memory words for other variables. A variable is just a location in memory that is reserved for use as that variable. To use that variable, you will need to first load its value into a register. If the value changes, you will need to write its value back into the memory location if you want to keep it. The variables are declared in assembly language by using a .DATA directive, and using a label to specify a certain word of memory.

Pascal:

VAR
x, y : INTEGER;

BEGIN
x := 10;
y := x;
END;
C++:

main () {
int x, y;
x = 10;
y = x;
}
Assembly:

SET r1, 10 ; set r1 to immediate value 10
STORE X, r1 ; store r1 into variable x

LOAD r1, X ; load variable x into r1
STORE Y, r1 ; store r1 into variable y

.DATA
X: 0 ; declare a memory location for variable x
Y: 0 ; declare a memory location for variable y

Introduction to Assembly Language

Assembly language, commonly called assembly or asm, is a human-readable notation for the machine language that a specific computer architecture uses. Machine language, a pattern of bits encoding machine operations, is made readable by replacing the raw values with symbols called mnemonics.

For example, a computer with the appropriate processor will understand this x86/IA-32 machine language:

10110000 01100001

For programmers, however, it is easier to remember the equivalent assembly language representation:

mov al, 061h

which means to move the hexadecimal value 61 (97 decimal) into the processor register with the name "al". The mnemonic "mov" is short for "move", and a comma-separated list of arguments or parameters follows it; this is a typical assembly language statement.

Transforming assembly into machine language is accomplished by an assembler, and the reverse by a disassembler. Unlike in high-level languages, there is usually a 1-to-1 correspondence between simple assembly statements and machine language instructions. However, in some cases an assembler may provide pseudoinstructions which expand into several machine language instructions to provide commonly needed functionality. For example, for a machine that lacks a "branch if greater or equal" instruction, an assembler may provide a pseudoinstruction that expands to the machine's "set if less than" and "branch if zero (on the result of the set instruction)".

Computer Articles and Tutorials
...PHP, MySQL, Perl...

Every computer architecture has its own machine language, and therefore its own assembly language. Computers differ by the number and type of operations they support. They may also have different sizes and numbers of registers, and different representations of data types in storage. While all general-purpose computers are able to carry out essentially the same functionality, the way they do it differs, and the corresponding assembly language must reflect these differences.

In addition, multiple sets of mnemonics or assembly-language syntax may exist for a single instruction set. In these cases, the most popular one is usually that used by the manufacturer in their documentation.

ASCII Codes



This is an example of ASCII codes. This codes are Helpful for the Assembly Language.
It could help determine the Codes for the letters.