Browse Prior Art Database

Code optimization by self modification in load-store instruction set machines Disclosure Number: IPCOM000199991D
Publication Date: 2010-Sep-23
Document File: 9 page(s) / 76K

Publishing Venue

The Prior Art Database


Disclosed is a technique for code optimization in load-store instruction set machines. The article aims at achieving optimization by self modification of code. Most instructions that need memory references i.e direct or indirect addressing are replaced with their register or immediate equivalents. The optimization is achieved without the need of an extra register or specialized hardware.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 48% of the total text.

Page 01 of 9

Code optimization by self modification in load -store instruction set machines

Load-store style architecture is common in many machines most notably the x86

machines by Intel and AMD. A very important feature of this kind of architecture is that it

forbids in most cases, transfer of data between memory. Most transfer takes place through

registers. Registers are small, very high speed memory. Instructions get executed faster in

registers than in memory. Registers are however limited in capacity as well as in number.
Code optimization is a technique in which code is altered in a way such that it brings

improvements in some dimension such as CPU usage, memory or speed. There are several

factors that determine code optimization. One of the factors is the number of registers available.

Code optimization is an integral part of compiler optimization and is also important in critical

Self modifying code is a style of coding where code in high-level or machine level form

adapts itself to suit the need of the application and in general brings optimization. Self

modification may take place at a static level before a run or during run i.e on the fly. In assembly

language, self modifying code can exist more freely.
This article deals with load-store architecture in particular. The central idea is to bring

code optimization in such systems both before and at run time by trying to eliminate as much

memory usage as possible. For this, normal assembly level code is transformed into self

modifying code.
While running, this code replaces most code in memory addressing mode (in various

different modes like direct, indirect, indexed etc) into absolute or register addressing modes.
The aim is to enhance speed, as most operations will be performed in the registers than


Consider a very simple assembly code snippet in x86 language for Microsoft Assembler. mov ax, 1234h

mov bx, ax
This code part deals entirely with registers. As a result the operation is quick.



consider the following example.

.MODEL Small


msg db 'Hello, world!$'



Page 02 of 9


mov ah, 09h

lea dx, msg

int 21h

mov ax,4C00h

int 21h

end start

Here the string 'Hello, world!' is stored in a memory location named 'db'. The contents of

this location i.e 'Hello, world!' are then loaded into dx register. By replacing the instruction lea dx, msg ...............with its immediate counterpart i.e
lea dx,'Hello, world!$'.............we have faster execution.

This is not complete however. Data in memory may need to be read again and again. In

this case our self modifier adapts accordingly. Every reference to this location is changed to its

immediate counterpart.

However, the scenario gets tougher when we come across data that changes or is


Consider the following example.

.model small .data

Fname db 'my.txt',00h

Handle dw ?

    Buf db 80h dup(?) .stack 100h
mov ax,@data
mov ds,ax
mov ah, 3dh


Page 03 of 9

mov al, 0
lea dx, Fname int 21h
mov Handle, ax...