Algorithm and Programming Week4
Algorithm and Programming Week4
and
Programming
VI190207
Subject of the
Lectures
• C structure • Input / Output Operation
• Variable and contanta • Branc and Loop
• Operator and expression • Array, string and pointer
C Structure
Declaration .data
; Va r i a b l e and a r r a y d e c l a r a t i o n s
; Declare v a r i a b l e s a t t h i s l e v e l
; O t h e r P r o ce du r es
; Always o r g a n i z e y o u r program i n t o p r o c e d u r e s
end main ; To mark t h e end o f t h e s o u r c e f i l e
Model Directives
C Structure
Structure of a C Program vs Asssembly Program
Asssembly Program
%macro write_string 2
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
mov ecx, %1 ; message to write -> mov ecx, msg
mov edx, %2 ; message length -> mov edx, len
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello World!',0xA,0xD
len1 equ $ - msg1 ; $ evaluates to the "current address",
;so $ - msg1 means "the current address
;minus the address with the label msg1".
;This calculates the length of the string
;that starts at msg1
C Structure
Structure of a C Program
C Structure
comment of a C Program
Block Comments
Line Comments
Algorithm and
Programming
VI190207
Identifiers
bool fact;
short maxItems; //Word separator : Capital
long long national_debt; //Word separator : underscore
float payRate; //Word separator : Capital
double tax;
char code, kind; //Poor style – see text
int a, b; //Poor style – see text
Variable and Constanta
Variable Initialization
Program Memory
Constant Representation
Coding Constants
Note
A character constant is enclosed in single quotes.
Variable and Constanta
Constants – control character
Variable and Constanta
Constants – integer
example
Constants –
real example
Variable and Constanta
Constants – string
example
Note
Use single quotes for character constants.
Use double quotes for string constants.
Algorithm and
Programming
VI190207
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition
>= (A >= B) is not true.
becomes true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition
<= (A <= B) is true.
becomes true.
Operator and expression
Operators in C:
The operators can be classified as: Operator Description
Arithmetic Operators && Called Logical AND operator. If both the
Increment and Decrement Operators operands are non-zero, then condition
Assignment Operators becomes true.
Relational Operators
Example : (A && B) is false.
Logical Operators
Conditional Operators || Called Logical OR Operator. If any of
Bitwise Operators the two operands is non-zero, then
Special Operators condition becomes true.
Example : (A || B) is true.
! Called Logical NOT Operator. Use to
reverses the logical state of its operand.
If a condition is true, then Logical NOT
operator will make false.
Example : !(A && B) is true
Operator and expression
Operators in C:
The operators can be classified as: Category Operator Associativity
Arithmetic Operators Postfix () [] -> . ++ - - Left to right
Increment and Decrement Operators Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Assignment Operators Multiplicative * / % Left to right
Relational Operators Additive +- Left to right
Logical Operators Shift << >> Left to right
Conditional Operators Relational < <= > >= Left to right
Bitwise Operators Equality == != Left to right
Special Operators Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right Logical OR ||
Left to right Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma
, Left to right
Operator and expression
Operators in C:
The operators can be classified as: Bitwise operator works on bits and performs bit-by-bit
Arithmetic Operators operation. The truth tables for &, |, and ^ are as follows:
Increment and Decrement Operators
Assignment Operators P q p&q p|q p^q
Relational Operators
0 0 0 0 0
Logical Operators
Conditional Operators 0 1 0 1 1
Bitwise Operators 1 1 1 1 0
Special Operators 1 0 0 1 1
Operator and expression
Operators in C:
The operators can be classified as: Category Operator Associativity
Arithmetic Operators Postfix () [] -> . ++ - - Left to right
Increment and Decrement Operators Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Assignment Operators Multiplicative * / % Left to right
Relational Operators Additive +- Left to right
Logical Operators Shift << >> Left to right
Conditional Operators Relational < <= > >= Left to right
Bitwise Operators Equality == != Left to right
Special Operators Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right Conditional ?:
Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |=
Right to left
Comma , Left to right
The End