SlideShare a Scribd company logo
1 of 21
ASSEMBLY LANGUAGE FUNDAMENTALS
SANGRAM KESARI RAY <SHANKAR.RAY030@GMAIL.COM>
INTEGER LITERALS/CONSTANTS
• DIFFERENT TYPES OF REPRESENTATIONS
• 10 //decimal
• 10d //decimal
• 10b //binary
• 10q //octal
• 10o //octal
• 10h //hexadecimal
• 0a10h //hexadecimal, a hexadecimal literal beginning with a letter must have a leading 0
Arithmetic
Operators
Precedence
1. Parentheses ()
2. Unary plus, minus +, -
3. Multiply, Divide *, / 3. Modulus MOD
4. Add, Subtract +, -
REAL NUMBER/FLOATING POINT
• IEEE 32-BIT FORMAT
• 1-BIT //SIGN BIT
• 8-BIT //EXPONENT
• 23-BIT //MANTISSA
• WRITTEN IN PROGRAM AS //[sign]integer.[integer][exponent]
• 12.
• -10.1
• -3.
• +15.8
• As data is stored as binary in the memory, we use 32-bit IEEE format
• 12.375D is stored as 01000001010001100000000000000000B
STEP 1
TAKE OUT THE INTEGER PART INTO BINARY //12D -> 1100B
ALGORITHM
TMP = FRACTIONAL PART //.375
TMP *= 2 //0.750 // TAKE OUT THE INTEGRAL PART FROM TMP => 0
WHILE TMP = ZERO // DO ABOVE
// 2*0.750 = 1.500 => TAKE OUT 1
// 2*0.500 = 1.000 => TAKE OUT 1
STEP 2
12.375D BECOME 1100.011B //
Step 3
IEEE REPRESENTATION OF 1100.011B IS 1.100011B x 23
EXPONENT 3 BECOMES 3+127(8-BIT SIGNED) = 130D = 10000010B //8-BIT
MANTISSA 100011B BECOMES 10001100000000000000000B //23-BIT
SIGN-BIT //1-BIT
01000001010001100000000000000000B // SIGN-BIT + EXPONENT + MANTISSA
HOW THE HECK 12.375Dis stored as 01000001010001100000000000000000B!
WTF!
CHARACTER LITERALS/CONSTANTS
• INSIDE SINGLE/DOUBLE QUOTS AS A, B, C, ETC.
• STORED AS BINARY IN THE MEMORY
STRING LITERALS/CONSTANTS
• SEQUENCE OF CHARACTER LITERALS
IDENTIFIERS
• IT CAN BE A VARIALBLE, A CONSTANT, A PROCEDURE OR A LABEL
DIRECTIVES
• THEY ARE LIKE COMMANDS TO THE ASSEMBLER
• var1 BYTE 127 //this reserves space of 1-BYTE for var1
• .data //program segment/section to define variables
• .data? //segment contains un-initialized data
• .code //segment containing instructions
INSTRUCTIONS
• BASIC FORMAT - [label:] mnemonic [operands] [;comment] // ANYTHING INSIDE SQUARE BRACKETS IS OPTIONAL
• LETS SEE SOME SAMPLE INSTRUCTIONS WITH 0, 1, 2 AND 3 OPERANDS
• STC //SET CARRY FLAG
• INC EAX //INCREMENT EAX BY 1
• MOV EAX, 5 //STORE 5 IN EAX REGISTER
• IMUL EAX, EBX, 5 //MULTIPLY EBX BY 5 AND STORE IN EAX REGISTER
Assembling,
Linking and
Running
1. Source file
2. Assembler
3. Linker
4. Loader
1. Write the program as
source file
2. Assembler creates and
object file out of the source
file
3. Linker copies the
procedures from link
libraries and combines then
with object file and crates
an executable file
4. Loader reads the
executable file into memory
and redirects the CPU to
program’s starting address
DEFINITING DATA
• The following directives allocate the corresponding space to the identifier/variables
• VAR1 BYTE 10 //ALLOCATE 8-BIT
• VAR2 SBYTE 10 //8-BIT SIGNED
• VAR3 WORD 10 //16-BIT
• VAR4 SWORD 10 //16-BIT SIGNED
• VAR5 DWORD 10 //32-BIT
• VAR6 SDWORD //32-BIT SIGNED
• VAR7 FWORD //48-BIT FAR POINTER
• VAR8 QWORD //64-BIT
• VAR9 TBYTE //80-BIT
• VAR10 REAL4 //32-BIT IEEE SHORT REAL (1-BIT SIGNED + 8-BIT EXPONENT + 32-BIT MANTISSA)
• VAR11 REAL8 //64-BIT IEEE LONG REAL (1-BIT SIGNED + 11-BIT EXPONENT + 52-BIT MANTISSA)
• VAR12 REAL10 //80-BIT IEEE EXTENDED REAL (1-BIT SIGNED + 15-BIT EXPONENT + 1-BIT INTEGER + 63-BIT
MANTISSA)
• VAR13 DB //8-BIT
• VAR14 DW //16-BIT
• VAR15 DD //32-BIT OR REAL
• VAR16 DQ //64-BIT OR REAL
• VAR17 DT //80-BIT
BYTE AND FRIENDS
Single Initializer
VAR1 BYTE 10 //ALLOCATE 8-BIT
VAR2 SBYTE 10 //8-BIT SIGNED
VAR3 BYTE ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME
VAR4 DB 10 //OLD-SCHOOL
VAR5 BYTE “Hello-world”,0 //A NULL terminated string with double-quotes
VAR6 BYTE ‘Bye-world”,0 //Another NULL terminal string with single-quotes
Multiple Initializers
myArray BYTE 10, 11, 12 //1-D Array
myMatrix SBYTE 20, 21, 22 //2-D Matrix
BYTE 23, 24, 25
SBYTE 26, 27, 28
myArray2 BYTE 10 DUP (0) //allocates 10-BYTES and set all bytes to zero
myArray3 BYTE 10 DUP (?) //allocates 10-BYTES, un-initialized
WORD AND FRIENDS
Single Initializer
VAR1 WORD 10 //ALLOCATE 16-BIT
VAR2 SWORD 10 //16-BIT SIGNED
VAR3 WORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME
VAR4 DW 10 //OLD-SCHOOL
Multiple Initializers
myArray WORD 10, 11, 12 //1-D Array
myMatrix SWORD 20, 21, 22 //2-D Matrix
WORD 23, 24, 25
SWORD 26, 27, 28
myArray2 WORD 10 DUP (0) //allocates 10-WORDS and set all WORDs to zero
myArray3 WORD 10 DUP (?) //allocates 10-WORDS, un-initialized
DWORD AND FRIENDS
Single Initializer
VAR1 DWORD 10 //ALLOCATE 32-BIT
VAR2 SDWORD 10 //32-BIT SIGNED
VAR3 DWORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME
VAR4 DD 10 //OLD-SCHOOL
Multiple Initializers
myArray DWORD 10, 11, 12 //1-D Array
myMatrix SDWORD 20, 21, 22 //2-D Matrix
DWORD 23, 24, 25
SDWORD 26, 27, 28
myArray2 DWORD 10 DUP (0) //allocates 10-DWORDS and set all DWORDs to zero
myArray3 DWORD 10 DUP (?) //allocates 10-DWORDS, un-initialized
QWORD AND FRIENDS
Single Initializer
VAR1 QWORD 10 //ALLOCATE 64-BIT
VAR2 QWORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME
VAR3 DQ 10 //OLD-SCHOOL
Multiple Initializers
myArray QWORD 10, 11, 12 //1-D Array
myMatrix QWORD 20, 21, 22 //2-D Matrix
QWORD 23, 24, 25
QWORD 26, 27, 28
myArray2 QWORD 10 DUP (0) //allocates 10-QWORDS and set all QWORDs to zero
myArray3 QWORD 10 DUP (?) //allocates 10-QWORDS, un-initialized
TBYTE/PACKED BCD(BINARY CODED DECIMAL)
10-BYTES or 80-BIT //size
each half-byte of lower 9-bytes contains a single decimal digit
the highest bit of the highest byte contains sign-bit 0/1 (+/-)
lower 7-bits of the highest bytes are not used
+999999999999999999 //18-digits, max value
-999999999999999999 //18-digits, min value
I encourage the reader to figure out how BCD can be used in LED-display.
REAL FRIENDS
Single Initializer
VAR1 REAL4 1.0 //4-BYTE
VAR2 DD 1.0 //4-BYTE
VAR3 REAL8 ? //8-BYTE
VAR4 DQ ? //8-BYTE
VAR5 REAL10 1.0 //10-BYTE
VAR6 DT 1.0 //10-BYTE
Multiple Initializers
myArray REAL4 1.0, 1.1, 1.2 //1-D Array
myMatrix REAL4 2.0, 2.1, 2.2 //2-D Matrix
REAL8 2.3, 2.4, 2.5
REAL10 2.6, 2.7, 2.8
myArray2 REAL4 1.0 DUP (0) //allocates 10-REAL4 and set all REAL4 to zero
myArray3 REAL4 1.0 DUP (?) //allocates 10-REAL4, un-initialized
LITTLE-ENDIAN VS BIG-ENDIAN
1. CPU STORES/RETRIEVE LEAST SIGNIFICANT BYTE FIRST //LITTLE
2. CPU STORES /RETRIEVE THE MOST SIGNIFICANT BYTE FIRST //BIG
0x11223344
Little (LSB First) 44 33 22 11
Memory 0 1 2 3 4 5 6 7 8
Big (MSB First) 11 22 33 44
Memory 0 1 2 3 4 5 6 7 8
SYMBOLIC CONSTANTS
They are associating an identifier with an integer, text, etc.
e.g.
mov eax, 10 //no symbolic constant
tmp = 10
mov eax, tmp //using symbolic constant
Symbolic constants allows more maintainable programs, like storing value of PI = 3.14
CONT.
Using Assembler defined Symbolic constants: $, EQU and TEXTEQU
Finding string length
tmp = "Hello-world"
tmpLen = ($ - tmp)
Defining symbols as constants throughout the program //no re-definition allowed
PI EQU <3.14>
Creating textmacro //tiny code-chunks
addTenEAX TEXTEQU <add eax, 10>
Both these .code segments are same
.code
add eax, 10
.code
addTenEAX
DEMO
Even-odd
https://github.com/shankar-ray/Assembly-Language-Tutorials-for-
Windows/tree/master/03%20Assembly%20Language

More Related Content

Similar to Assembly Language Tutorials for Windows - 03 Assembly Language Programming

Cics TS 5.1 user experience
Cics TS 5.1 user experienceCics TS 5.1 user experience
Cics TS 5.1 user experience
Larry Lawler
 
Demystifying Secure enclave processor
Demystifying Secure enclave processorDemystifying Secure enclave processor
Demystifying Secure enclave processor
Priyanka Aash
 
10x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp0210x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp02
promethius
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance Improvements
Ronald Bradford
 
Apache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual HostingApache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual Hosting
webhostingguy
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @Media
Tom Croucher
 
About Multiblock Reads v4
About Multiblock Reads v4About Multiblock Reads v4
About Multiblock Reads v4
Enkitec
 
InnoDB: архитектура транзакционного хранилища (Константин Осипов)
InnoDB: архитектура транзакционного хранилища (Константин Осипов)InnoDB: архитектура транзакционного хранилища (Константин Осипов)
InnoDB: архитектура транзакционного хранилища (Константин Осипов)
Ontico
 

Similar to Assembly Language Tutorials for Windows - 03 Assembly Language Programming (20)

Cics TS 5.1 user experience
Cics TS 5.1 user experienceCics TS 5.1 user experience
Cics TS 5.1 user experience
 
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra  SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
8085 Microprocessor - Ramesh Gaonkar.pdf-27 (1).pptx
8085 Microprocessor - Ramesh Gaonkar.pdf-27 (1).pptx8085 Microprocessor - Ramesh Gaonkar.pdf-27 (1).pptx
8085 Microprocessor - Ramesh Gaonkar.pdf-27 (1).pptx
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
Embedded system classes in mumbai
Embedded system classes in mumbaiEmbedded system classes in mumbai
Embedded system classes in mumbai
 
GDC2014: Boosting your ARM mobile 3D rendering performance with Umbra
GDC2014: Boosting your ARM mobile 3D rendering performance with Umbra GDC2014: Boosting your ARM mobile 3D rendering performance with Umbra
GDC2014: Boosting your ARM mobile 3D rendering performance with Umbra
 
Demystifying Secure enclave processor
Demystifying Secure enclave processorDemystifying Secure enclave processor
Demystifying Secure enclave processor
 
Assembler Language Tutorial for Mainframe Programmers
Assembler Language Tutorial for Mainframe ProgrammersAssembler Language Tutorial for Mainframe Programmers
Assembler Language Tutorial for Mainframe Programmers
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
10x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp0210x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp02
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance Improvements
 
Apache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual HostingApache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual Hosting
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @Media
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
Happy To Use SIMD
Happy To Use SIMDHappy To Use SIMD
Happy To Use SIMD
 
About Multiblock Reads v4
About Multiblock Reads v4About Multiblock Reads v4
About Multiblock Reads v4
 
InnoDB: архитектура транзакционного хранилища (Константин Осипов)
InnoDB: архитектура транзакционного хранилища (Константин Осипов)InnoDB: архитектура транзакционного хранилища (Константин Осипов)
InnoDB: архитектура транзакционного хранилища (Константин Осипов)
 
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
 

More from Sangram Kesari Ray

More from Sangram Kesari Ray (6)

Assembly Language Tutorials for Windows - 05 Procedures Part 1
Assembly Language Tutorials for Windows - 05 Procedures Part 1Assembly Language Tutorials for Windows - 05 Procedures Part 1
Assembly Language Tutorials for Windows - 05 Procedures Part 1
 
Assembly Language Tutorials for Windows - 04 Data Transfers Part-3
Assembly Language Tutorials for Windows - 04 Data Transfers Part-3Assembly Language Tutorials for Windows - 04 Data Transfers Part-3
Assembly Language Tutorials for Windows - 04 Data Transfers Part-3
 
Assembly Language Tutorials for Windows - 04 Data Transfers Part-1
Assembly Language Tutorials for Windows - 04 Data Transfers Part-1Assembly Language Tutorials for Windows - 04 Data Transfers Part-1
Assembly Language Tutorials for Windows - 04 Data Transfers Part-1
 
Assembly Language Tutorials for Windows - 02 x86-64 Architecture
Assembly Language Tutorials for Windows - 02 x86-64 ArchitectureAssembly Language Tutorials for Windows - 02 x86-64 Architecture
Assembly Language Tutorials for Windows - 02 x86-64 Architecture
 
Assembly Language Tutorials for Windows - 01 Computer Programming
Assembly Language Tutorials for Windows - 01 Computer ProgrammingAssembly Language Tutorials for Windows - 01 Computer Programming
Assembly Language Tutorials for Windows - 01 Computer Programming
 
03 Win32 API - Creating a Window
03 Win32 API - Creating a Window03 Win32 API - Creating a Window
03 Win32 API - Creating a Window
 

Recently uploaded

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

Assembly Language Tutorials for Windows - 03 Assembly Language Programming

  • 1. ASSEMBLY LANGUAGE FUNDAMENTALS SANGRAM KESARI RAY <SHANKAR.RAY030@GMAIL.COM>
  • 2. INTEGER LITERALS/CONSTANTS • DIFFERENT TYPES OF REPRESENTATIONS • 10 //decimal • 10d //decimal • 10b //binary • 10q //octal • 10o //octal • 10h //hexadecimal • 0a10h //hexadecimal, a hexadecimal literal beginning with a letter must have a leading 0
  • 3. Arithmetic Operators Precedence 1. Parentheses () 2. Unary plus, minus +, - 3. Multiply, Divide *, / 3. Modulus MOD 4. Add, Subtract +, -
  • 4. REAL NUMBER/FLOATING POINT • IEEE 32-BIT FORMAT • 1-BIT //SIGN BIT • 8-BIT //EXPONENT • 23-BIT //MANTISSA • WRITTEN IN PROGRAM AS //[sign]integer.[integer][exponent] • 12. • -10.1 • -3. • +15.8 • As data is stored as binary in the memory, we use 32-bit IEEE format • 12.375D is stored as 01000001010001100000000000000000B
  • 5. STEP 1 TAKE OUT THE INTEGER PART INTO BINARY //12D -> 1100B ALGORITHM TMP = FRACTIONAL PART //.375 TMP *= 2 //0.750 // TAKE OUT THE INTEGRAL PART FROM TMP => 0 WHILE TMP = ZERO // DO ABOVE // 2*0.750 = 1.500 => TAKE OUT 1 // 2*0.500 = 1.000 => TAKE OUT 1 STEP 2 12.375D BECOME 1100.011B // Step 3 IEEE REPRESENTATION OF 1100.011B IS 1.100011B x 23 EXPONENT 3 BECOMES 3+127(8-BIT SIGNED) = 130D = 10000010B //8-BIT MANTISSA 100011B BECOMES 10001100000000000000000B //23-BIT SIGN-BIT //1-BIT 01000001010001100000000000000000B // SIGN-BIT + EXPONENT + MANTISSA HOW THE HECK 12.375Dis stored as 01000001010001100000000000000000B!
  • 7. CHARACTER LITERALS/CONSTANTS • INSIDE SINGLE/DOUBLE QUOTS AS A, B, C, ETC. • STORED AS BINARY IN THE MEMORY STRING LITERALS/CONSTANTS • SEQUENCE OF CHARACTER LITERALS
  • 8. IDENTIFIERS • IT CAN BE A VARIALBLE, A CONSTANT, A PROCEDURE OR A LABEL DIRECTIVES • THEY ARE LIKE COMMANDS TO THE ASSEMBLER • var1 BYTE 127 //this reserves space of 1-BYTE for var1 • .data //program segment/section to define variables • .data? //segment contains un-initialized data • .code //segment containing instructions
  • 9. INSTRUCTIONS • BASIC FORMAT - [label:] mnemonic [operands] [;comment] // ANYTHING INSIDE SQUARE BRACKETS IS OPTIONAL • LETS SEE SOME SAMPLE INSTRUCTIONS WITH 0, 1, 2 AND 3 OPERANDS • STC //SET CARRY FLAG • INC EAX //INCREMENT EAX BY 1 • MOV EAX, 5 //STORE 5 IN EAX REGISTER • IMUL EAX, EBX, 5 //MULTIPLY EBX BY 5 AND STORE IN EAX REGISTER
  • 10. Assembling, Linking and Running 1. Source file 2. Assembler 3. Linker 4. Loader 1. Write the program as source file 2. Assembler creates and object file out of the source file 3. Linker copies the procedures from link libraries and combines then with object file and crates an executable file 4. Loader reads the executable file into memory and redirects the CPU to program’s starting address
  • 11. DEFINITING DATA • The following directives allocate the corresponding space to the identifier/variables • VAR1 BYTE 10 //ALLOCATE 8-BIT • VAR2 SBYTE 10 //8-BIT SIGNED • VAR3 WORD 10 //16-BIT • VAR4 SWORD 10 //16-BIT SIGNED • VAR5 DWORD 10 //32-BIT • VAR6 SDWORD //32-BIT SIGNED • VAR7 FWORD //48-BIT FAR POINTER • VAR8 QWORD //64-BIT • VAR9 TBYTE //80-BIT • VAR10 REAL4 //32-BIT IEEE SHORT REAL (1-BIT SIGNED + 8-BIT EXPONENT + 32-BIT MANTISSA) • VAR11 REAL8 //64-BIT IEEE LONG REAL (1-BIT SIGNED + 11-BIT EXPONENT + 52-BIT MANTISSA) • VAR12 REAL10 //80-BIT IEEE EXTENDED REAL (1-BIT SIGNED + 15-BIT EXPONENT + 1-BIT INTEGER + 63-BIT MANTISSA) • VAR13 DB //8-BIT • VAR14 DW //16-BIT • VAR15 DD //32-BIT OR REAL • VAR16 DQ //64-BIT OR REAL • VAR17 DT //80-BIT
  • 12. BYTE AND FRIENDS Single Initializer VAR1 BYTE 10 //ALLOCATE 8-BIT VAR2 SBYTE 10 //8-BIT SIGNED VAR3 BYTE ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME VAR4 DB 10 //OLD-SCHOOL VAR5 BYTE “Hello-world”,0 //A NULL terminated string with double-quotes VAR6 BYTE ‘Bye-world”,0 //Another NULL terminal string with single-quotes Multiple Initializers myArray BYTE 10, 11, 12 //1-D Array myMatrix SBYTE 20, 21, 22 //2-D Matrix BYTE 23, 24, 25 SBYTE 26, 27, 28 myArray2 BYTE 10 DUP (0) //allocates 10-BYTES and set all bytes to zero myArray3 BYTE 10 DUP (?) //allocates 10-BYTES, un-initialized
  • 13. WORD AND FRIENDS Single Initializer VAR1 WORD 10 //ALLOCATE 16-BIT VAR2 SWORD 10 //16-BIT SIGNED VAR3 WORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME VAR4 DW 10 //OLD-SCHOOL Multiple Initializers myArray WORD 10, 11, 12 //1-D Array myMatrix SWORD 20, 21, 22 //2-D Matrix WORD 23, 24, 25 SWORD 26, 27, 28 myArray2 WORD 10 DUP (0) //allocates 10-WORDS and set all WORDs to zero myArray3 WORD 10 DUP (?) //allocates 10-WORDS, un-initialized
  • 14. DWORD AND FRIENDS Single Initializer VAR1 DWORD 10 //ALLOCATE 32-BIT VAR2 SDWORD 10 //32-BIT SIGNED VAR3 DWORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME VAR4 DD 10 //OLD-SCHOOL Multiple Initializers myArray DWORD 10, 11, 12 //1-D Array myMatrix SDWORD 20, 21, 22 //2-D Matrix DWORD 23, 24, 25 SDWORD 26, 27, 28 myArray2 DWORD 10 DUP (0) //allocates 10-DWORDS and set all DWORDs to zero myArray3 DWORD 10 DUP (?) //allocates 10-DWORDS, un-initialized
  • 15. QWORD AND FRIENDS Single Initializer VAR1 QWORD 10 //ALLOCATE 64-BIT VAR2 QWORD ? //UN-INITIALIZED, TO-BE ASSIGNED AT RUN-TIME VAR3 DQ 10 //OLD-SCHOOL Multiple Initializers myArray QWORD 10, 11, 12 //1-D Array myMatrix QWORD 20, 21, 22 //2-D Matrix QWORD 23, 24, 25 QWORD 26, 27, 28 myArray2 QWORD 10 DUP (0) //allocates 10-QWORDS and set all QWORDs to zero myArray3 QWORD 10 DUP (?) //allocates 10-QWORDS, un-initialized
  • 16. TBYTE/PACKED BCD(BINARY CODED DECIMAL) 10-BYTES or 80-BIT //size each half-byte of lower 9-bytes contains a single decimal digit the highest bit of the highest byte contains sign-bit 0/1 (+/-) lower 7-bits of the highest bytes are not used +999999999999999999 //18-digits, max value -999999999999999999 //18-digits, min value I encourage the reader to figure out how BCD can be used in LED-display.
  • 17. REAL FRIENDS Single Initializer VAR1 REAL4 1.0 //4-BYTE VAR2 DD 1.0 //4-BYTE VAR3 REAL8 ? //8-BYTE VAR4 DQ ? //8-BYTE VAR5 REAL10 1.0 //10-BYTE VAR6 DT 1.0 //10-BYTE Multiple Initializers myArray REAL4 1.0, 1.1, 1.2 //1-D Array myMatrix REAL4 2.0, 2.1, 2.2 //2-D Matrix REAL8 2.3, 2.4, 2.5 REAL10 2.6, 2.7, 2.8 myArray2 REAL4 1.0 DUP (0) //allocates 10-REAL4 and set all REAL4 to zero myArray3 REAL4 1.0 DUP (?) //allocates 10-REAL4, un-initialized
  • 18. LITTLE-ENDIAN VS BIG-ENDIAN 1. CPU STORES/RETRIEVE LEAST SIGNIFICANT BYTE FIRST //LITTLE 2. CPU STORES /RETRIEVE THE MOST SIGNIFICANT BYTE FIRST //BIG 0x11223344 Little (LSB First) 44 33 22 11 Memory 0 1 2 3 4 5 6 7 8 Big (MSB First) 11 22 33 44 Memory 0 1 2 3 4 5 6 7 8
  • 19. SYMBOLIC CONSTANTS They are associating an identifier with an integer, text, etc. e.g. mov eax, 10 //no symbolic constant tmp = 10 mov eax, tmp //using symbolic constant Symbolic constants allows more maintainable programs, like storing value of PI = 3.14
  • 20. CONT. Using Assembler defined Symbolic constants: $, EQU and TEXTEQU Finding string length tmp = "Hello-world" tmpLen = ($ - tmp) Defining symbols as constants throughout the program //no re-definition allowed PI EQU <3.14> Creating textmacro //tiny code-chunks addTenEAX TEXTEQU <add eax, 10> Both these .code segments are same .code add eax, 10 .code addTenEAX