Sie sind auf Seite 1von 4

CMSC 132 • Reserve an array of 10 reals

ASSEMBLY PROGRAMMING • The .text Section
• This is where the actual assembly code is written.
• Must begin with the declaration global _start, 
Intro to Linux Assembly which just tells the kernel where the program 
execution begins.
    Main Difference between DOS and Linux Assembly
DOS Linux section .text
global _start
Interrupt call int 21h int 80h
Bit  16­bit assembly 32­bit protected  _start:
mode assembly pop ebx
.
you never need to use  .
a segment override  .
or modify any 
segment register, and 
every address is 32 
bits long and  Linux System Calls
contains only an 
offset part
Linux system calls are called in exactly the same way as DOS 
Registers 16­bit registers 32­bit registers system calls: 
AX, BX, CX, etc. EAX, EBX, ECX, 
etc 1. You put the system call number in EAX (we're dealing 
with 32­bit registers here, remember) 
Usage Obsolete Currently used
 
2. You set up the arguments to the system call in EBX, ECX, 
etc. 
    Parts of Assembly Program
• The .data Section 3. You call the relevant interrupt (for DOS, 21h; for Linux, 
• defining variables 80h) 
• defining constants 4. The result is usually returned in EAX 
• use DB, DW, DD, DQ and DT instructions There are six registers that are used for the arguments that the system 
call takes. The first argument goes in EBX, the second in ECX, then 
section .data EDX, ESI, EDI, and finally EBP, if there are so many.
message: db 'Hello world!'
msglength: equ 12 Linux System Call Table
buffersize: dw 1024 http://bluemaster.iu.hio.no/edu/ca/lin­asm/syscalls.html

• Declare message to contain the bytes 'Hello world!' 
(without quotes)
• Declare msglength to have the constant value 12
• Declare buffersize to be a word containing 1024
• The .bss Section
• define variables
• use RESB, RESW, RESD, RESQ and REST 
instructions to reserve unintialized space in memory 
of your variables.

section .bss
filename: resb 255
number: resb 1
bignum: resw 1
realarray: resq 10

• Reserve 255 bytes
• Reserve 1 byte
• Reserve 1 word (1 word = 2 bytes)
NASM vs GAS

NASM GAS
Comment ; This is a comment # This is a comment
Data Segment section .data    .section .data
    
BSS Segment section .bss .section .bss
Text Segment section .text .section .text
Global start global _start .globl _start
_start: _start:
Source­dest  op­code dest, src Op­code src, dest
operands
Immediate  push 4 pushl $4
Operands
Register  add eax, ebx addl %ebx, %eax
Operands
Variables var1 dd 40 var1: .int 40
Variable  db, dw, dd, dq and dt .byte, .int, .long, 
declaration  equ .ascii, .asciz, .string
directives 
(data section)
Variable  resb, resw, resq .lcomm
declaration 
directives (bss  e.g., e.g.,
section) add_resx: resb 1 .lcomm add_resx,1
String Length helloLen:  equ $­hello   helloLen:  .long 
.­hello
Addressing [ ] ( )
Compile nasm ­f elf program.o  as ­o program.o 
program.asm program.s
Hello World using GAS Syntax

.section .data
hello:     .ascii "Hello world!\0"
helloLen:  .long .­hello          

.section .bss
        .lcomm add_resx,1
.section .text

.globl _start:

movl $4, %eax
movl $1, %ebx
leal hello, %ecx
movl helloLen, %edx
int $0x80

movl $1, %eax
movl $0, %ebx
int $0x80
Sum of 2 1­digit No using NASM
;write input 2nd number
section .data mov eax,4            
hello:     dq 'Hello world!',10    ; 'Hello world!' plus  mov ebx,1            
a linefeed character mov ecx,input2        
helloLen:  equ $­hello             ; Length of the 'Hello  mov edx,input2Len     
world!' string                      
input1:    db 'Input 1st number: ', 0 int 80h              
input1Len: equ $­input1
input2:    db 'Input 2nd number: ', 0 ;get 2nd input number
input2Len: equ $­input2 mov eax,3            
output:    db  0  mov ebx,1            
outputLen: equ $­output                                   mov ecx,add_resy        
;mov edx,add_resy     
section .bss                      
add_resx: resd 1 int 80h              
add_resy: resd 1

section .text ;write 2nd input number
global _start mov eax,4            
mov ebx,1            
_start: mov ecx, add_resy     
mov eax,4            ; The system call for write  ;mov edx, add_resy   
(sys_write)                      
mov ebx,1            ; File descriptor 1 ­ standard  int 80h              
output
mov ecx,hello        ; Put the offset of hello in ecx
mov edx,helloLen     ; helloLen is a constant, so we  ;add x and y
don't need to say mov eax, [add_resx]
           ;  mov edx,[helloLen] to get it's actual value sub eax, 0x30
int 80h              ; Call the kernel
mov ebx, [add_resy]
;write input 1st number sub eax, 0x30
mov eax,4            
mov ebx,1             add eax, ebx
mov ecx,input1         add eax, 0x30
mov edx,input1Len     
                      mov [output], eax
int 80h              
;write 2nd input number
;get 1st input number mov eax,4           
mov eax,3             mov ebx,1            
mov ebx,1             mov ecx, output     
mov ecx,add_resx         mov edx, outputLen   
;mov edx,add_resx      int 80h              
                     
int 80h              
mov eax,1       ; The system call for exit (sys_exit)
;write 1st input number mov ebx,0      ; Exit with return code of 0 (no error)
mov eax,4             int 80h
mov ebx,1            
mov ecx, add_resx     
;mov edx, add_resx   
                     
int 80h              

Das könnte Ihnen auch gefallen