Physics 39 -- Assembly
September 15, 2009 -- Tuesday
MOVINGDATA
Various sections of an assembly program:
1. data
2. bss - .comm -- common mem area
ex.
.comm symbol, length
- .lcomm -- local common mem area
3. text
Data types/directives
.ascii
.asciz
.byte
double
float
.int
.long
.octa
.quad
.short
.single
example:
*pi:
.float 3.14159
*size
.long 100, 150, 200, 250, 300
#declaration
.section .data
msg:
.ascii " This is a test message"
factors:
.double 37.45, 45.33, 12.30
height:
.int 54
length:
.int 62, 35, 47
.equ factor, 3 #static variable
.equ LINUX_SYS_CALL, 0x80
movl LINUX_SYS_CALL, %eax
buffer - continuous block of bytes
how to show the memory occupied by a file
*vdir -- shows the memory occupied
by a file
*vdir -h - shows the memory
occupied by a file with the memory's
unit
Codes
1. smaller memory
#sizetest1.s
.section .bss
.lcomm buffer, 10000
.section .text
.globl main
main:
movl $1, %eax
movl $0, %ebx
int $0x80
*exit command
gcc -o sizetest1 sizetest1.s
./siztest1
2. bigger memory
#sizetest2.s
.section .data
buffer:
.fill 10000
.section .text
.globl main
main:
movl $1, %eax
movl $0, %ebx
int $0x80
gcc -o sizetest2 sizetest2.s
./sizetest2
MOV instruction
1. movl - 32 bit long word
2. movl - 16 bit word
3. movb - 8 bit byte
1 byte = 8 bits
example:
movl %eax, %ecx
movw %ax, %cx
movb %al, %cl
3. Debugging
#movtest1.s
.section .data
value
.int 1
.section .text
.globl main
main:
nop #no operation
movl value, %ecx
movl $1, %eax
movl $0, %ebx
int $0x80
--debugging--
gcc -o movtest movtest1.s -gstabs
man gcc
/-gstabs
/-gstabs
/-gstabs
q -- (quit)
1. gdb -q movtest1 -- starts the debugging process
2. (gdb) break *main+1 -- command use to begin the
debugging process in the main
3. run --command use to start debugging the main
4. --to print the value of ecx type:
print/x $ecx
5. s - command use to go to the next line
6. info registers -- show all the values which the variables
contain
4. looping
#movtest2.s
.section .data
output:
.asciz "The value is %d\n"
values:
.int 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
.section .text
.globl main
main:
nop
movl $0, %edi
loop:
movl
values
(, %edi, 4), %eax
pushl %eax
pushl $output
call printf
addl $8, %esp
inc %edi #inc - increment
cmpl $11, %edi #cmp - compare
jne loop #jne - jump not equal
movl $0, %ebx
movl $1, %eax
int $0x80
OUTPUT:
The value is 10
The value is 15
The value is 20
The value is 25
The value is 30
The value is 35
The value is 40
The value is 45
The value is 50
The value is 55
The value is 60
-----------------
*int - 32 bits
referencing
index system
4 things to consider in an
indexed memory mode:
1. base address
2. offset address
to add to the base address
*in movtest2 the offset address = 0.
3. size of data elements
4. index to determine which data element to select
----------------------------------------
in this line taken from movtest2.s
movl $0, %edi
if you change 0 to one the output would be:
The value is 15
The value is 20
The value is 25
The value is 30
The value is 35
The value is 45
The value is 50
The value is 55
The value is 60
-------------------------------------------
pushl %eax
pushl $output
call printf
addl $8, %esp
----------------------------------------
5. largest value in an array
#cmovtest.s
.section .data
output:
.asciz "The largest value is %d\n"
values:
.int 105, 235, 61, 315, 134, 221, 53, 145, 117, 5
.section .text
.globl main
main:
nop
movl values, %ebx
movl $0, %edi
loop:
movl values(, %edi, 4), %eax
cmp %ebx, %eax
cmova %eax, %ebx
inc %edi
cmp $10, %edi
jne loop
pushl %ebx
pushl $output
call printf
addl $8, %esp
pushl $0
call exit
gcc -o cmovtest cmovtest.s
./cmovtest
OUTPUT:
The largest value is 315
Igacitas: ENJOY Friends!
char! nose bleed sa assembly! hehehehe.. tc
ReplyDelete