Faculdade de Tecnologia de Americana
/home -> /home/fatec/ -> /home/fatec/sistcomp -> /home/fatec/sistcomp/assembly --::--
Brazilian Portuguese English

IA-32 Assembly - Linux

Prof. Rossano Pablo Pinto, MSc.





Tools (check the documentation via man)

Tool Description Example
as GNU Assembler as -gstabs -o cpuid.o cpuid.s
ld GNU Linker ld -o cpuid cpuid.o
gcc GNU Compiler (GNU Compiler Collection)
objdump Object Dump - prints object code information objdump -D cpuid.o
nm List symbols present in object files nm cpuid
ar Creates, modifies and extract files from a library archive
strings List strings from files strings cpuid
strip Remove symbols present in object files strip cpuid
gdb GNU Debugger
khexedit KDE Hexadecimal editor


Lista de Programas em Assembly IA-32


Tip: Verify the exit code specified at %ebx. After the execution of each program, execute the following command at the prompt:
echo $?



PROGRAM 1
⇒ Prints the processor vendor: cpuid.s
#cpuid.s Sample program to extract the processor Vendor ID
#Author: Richard Blum
.section .data
output:
   .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.globl _start
_start:
        nop
        mov  $0, %eax
        cpuid
        movl $output, %edi
        movl %ebx, 28(%edi)
        movl %edx, 32(%edi)
        movl %ecx, 36(%edi)
        movl $4, %eax         # USES SYSCALL 4 (WRITE) TO PRINT TO SCREEN
        movl $1, %ebx         # PRINTS TO STDOUT (FD 1)
        movl $output, %ecx    # TEXT ADDRESS
        movl $42, %edx        # TEXT LENGTH
        int  $0x80            # INVOKES LINUX SYSCALL

        movl $1, %eax         # USES SYSCALL 1 (EXIT) TO EXIT 
        movl $0, %ebx         # ERROR CODE = 0
        int  $0x80            # INVOKES LINUX SYSCALL

Generating the executable:
as -gstabs -o cpuid.o cpuid.s
ld -o cpuid cpuid.o
Running the program:
./cpuid
The processor Vendor ID is 'GenuineIntel'



PROGRAM 2
⇒ Prints processor information: cpuinfo.s
#cpuinfo.s Sample program to extract processor Info
#Author: Rossano Pablo Pinto
.section .data
vendor:
   .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"        # 42 bytes
brand: 
   .ascii "                                                \n" # 49 bytes
	
.section .text
.globl _start
_start:
	nop
	call vendorid
	call modelname
	call myexit
			
vendorid:
	########### VENDOR ID OPTION OF cpuid		
	mov  $0, %eax
	cpuid
	########### PUT ADDRESS OF vendor variable INTO edi REGISTER
	movl $vendor, %edi
	movl %ebx, 28(%edi)   
	movl %edx, 32(%edi)
	movl %ecx, 36(%edi)
	########### CALL LINUX PRINT SERVICE - SYSTEM CALL
	movl $4, %eax
	movl $1, %ebx
	movl $vendor, %ecx   ## POINTER TO START OF STRING
	movl $42, %edx       ## STRING SIZE
	int  $0x80
	ret
	
modelname:	
	movl $brand, %edi
	
	movl $0x80000002, %eax
	cpuid
	movl %eax, 0(%edi)
	movl %ebx, 4(%edi)
	movl %ecx, 8(%edi)
	movl %edx, 12(%edi)
	
	movl $0x80000003, %eax
	cpuid
	movl %eax, 16(%edi)
	movl %ebx, 20(%edi)
	movl %ecx, 24(%edi)
	movl %edx, 28(%edi)

	movl $0x80000004, %eax
	cpuid
	movl %eax, 32(%edi)
	movl %ebx, 36(%edi)
	movl %ecx, 40(%edi)
	movl %edx, 44(%edi)

        ########### CALL LINUX PRINT SERVICE - SYSTEM CALL
	movl $4, %eax      ##### WRITE SYSCALL
	movl $1, %ebx      ##### STDOUT
	movl $brand, %ecx
	movl $49, %edx     ## POINTER TO START OF STRING
	int  $0x80         ## STRING SIZE
	ret
	
	
myexit:			
	########### CALL LINUX EXIT SERVICE - SYSTEM CALL
	movl $1, %eax      ##### EXIT SYSCALL
	movl $0, %ebx      ##### EXIT CODE OF 0
	int  $0x80

Generating the executable:
as -gstabs -o cpuinfo.o cpuinfo.s
ld -o cpuinfo cpuinfo.o
Running the program:
./cpuinfo 
The processor Vendor ID is 'GenuineIntel'
Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHz



PROGRAM 3
⇒ Similar to 2: replace _start with main (uses gcc to generate code instead of "as" + "ld")
.
.
.
.globl main
main:
.
.
. 
Generating the executable:
gcc -o cpuinfo cpuinfo.s
Running the program:
./cpuinfo 
The processor Vendor ID is 'GenuineIntel'
Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHz



PROGRAM 4
⇒ Test the use of EFLAGS for conditional branch: cmptest.s
# cmptest.s - An example with the instructions CMP and JGE
# CMP sets EFLAGS accordingly 
# JGE verifies in EFLAGS if the value is greater than ...
#
# Theory:
#  Carry Flag (CF) - bit 0 (least significant bit)
#  Overflow Flag (OF) - bit 11
#  Parity Flag (PF) - bit 2
#  Sign Flag (SF) - bit 7
#  Zero Flag (ZF) - bit 6
#
#  JGE - Jump if greater or equal (SF=OF)
#	
.section .text
.globl _start
_start:
	nop
	movl $15, %eax
	movl $10, %ebx
	cmp %eax, %ebx
	jge MAIOR
	movl $1, %eax
	int $0x80
MAIOR:
	movl $20, %ebx
	movl $1, %eax
	int $0x80
Generating the executable:
as -gstabs -o cmptest.o cmptest.s
ld -o cmptest cmptest.o
Running the program:
./cmptest

Verify the exit code:
echo $?
10



PROGRAM 5
⇒ Test the use of EFLAGS for conditional branch: cmptest2.s
# cmptest2.s - An example with the instructions CMP and JGE
# CMP sets EFLAGS accordingly 
# JGE verifies in EFLAGS if the value is greater than ...
	
.section .text
.globl _start
_start:
	nop
	movl $10, %eax
	movl $15, %ebx
	cmp %eax, %ebx
	jge MAIOR
	movl $1, %eax
	int $0x80
MAIOR:
	movl $20, %ebx
	movl $1, %eax
	int $0x80
	
Generating the executable:
as -gstabs -o cmptest2.o cmptest2.s
ld -o cmptest2 cmptest2.o
Running the program:
 ./cmptest2

Verify the exit code:
echo $?
20




Links


- Art of Assembly Language Programming






Faculdade de Tecnologia de Americana :: Departamento de Processamento de Dados
Av. Nossa Senhora de Fátima, 567 - Tel. +55(19)3468-1049 CEP: 13478-540 / Americana / SP / Brasil
Copyleft 2007 Rossano Pablo Pinto