Chapter 9_ Assembly Language - Strings, Arrays, and Java Bytecodes
Chapter 9_ Assembly Language - Strings, Arrays, and Java Bytecodes
Language - Strings,
Arrays, and Java
Bytecodes
This chapter covers string manipulation, array operations, and Java
bytecodes.
by Tabeen Bokhat
2023-ce-13
That brings us to the end of my
presentation.
Thank you for your attention
String Primitive
Instructions
Move instructions
`MOVSB`, `MOVSW`, `MOVSD` transfer string data byte to dword.
Compare
instructions
`CMPSB`, `CMPSW`, `CMPSD` compare string elements, use repeat
prefixes.
Scan instructions
`SCAS*` scan strings by comparing accumulator with destination
data.
Selected String
Procedures
String length String copy Concatenation Case-insensitive
compare
Use `SCASB` to find null Loop with `MOVSB` to Find string end, then
terminator and calculate copy until null terminator copy second string after Convert characters to
Accessing
elements
Calculate offset: `base + (row*cols + col)*element_size` for
pointer access.
Searching Integer
Arrays
Linear search Binary search
Scan elements sequentially, compare each to target, Divide sorted array search space by half iteratively,
O(n) time. O(log n) time.
Sorting Integer
Arrays
Bubble sort Insertion sort
Repeatedly swap Build sorted section by
adjacent elements if out inserting elements in
of order, O(n²) order, O(n²) complexity.
complexity.
Optimization
Terminate early if no swaps happen in a pass, detecting
sorted array.
Java Bytecodes for
String Processing
Immutability Common
Java strings cannot be bytecodes
• `ldc`: load string literals
changed once created. • `new`: create objects
• `invokespecial`: invoke
constructors
• `invokevirtual`: call
string methods
Concatenation
Uses `StringBuilder` to append and convert back to string
efficiently.
Bytecode Analysis
Tools
javap IDE debuggers Decompilers
disassembler
List bytecode of class methods to Step through bytecode execution, Convert bytecode back to readable
understand compiled code inspect stack and locals in real- Java code for analysis and
structure. time. debugging.
Example: String
Reversal in Java
1 Create
Bytecode
StringBuilder
`new` and `dup` prepare a new object.
2 Initialize with
String
Constructor invoked with original string argument.
3 Reverse &
Convert
Call `reverse()` then `toString()` for reversed string.
4 Return result
`areturn` returns reversed string from the method.
Summary and
Conclusion
Assembly
mastery
Gain control over strings and arrays via primitive instructions.
2D arrays
Calculate addresses precisely for multidimensional access.
Java bytecode
insight
Understand how high-level string operations compile to
low-level code.
System-level
skills
Essential knowledge for optimizing and debugging
complex software.