; %include "asmtutor05x.asm"
SECTION .data
Mesaj db "Klavyeden girilen çoklu tamsayı argümanların toplamı: ", 0h
SECTION .text
global _start
_start:
pop ecx ; Klavyeden girilen argüman sayısı (program adı dahil)
pop edx ; Argüman 0 (program adı)
sub ecx, 1 ; Program adı sayaçtan düşülür
mov edx, 0 ; Argümanların toplamı kayıtçısı
birsonraki_arguman:
cmp ecx, 0h
jz argumanlar_bitti ; ECX=0 ise argümanlar bitmiştir, sonlandır
pop eax ; Klavyeden girilen aktüel argümanı yığından al
call adant ; AsciiDanTamsayıya fonksiyonunu çağırıp, aktüel argümanı tamsayıya çevir
add edx, eax ; EDX +=EAX (Aktüel tamsayı argümanı genele topla)
dec ecx ; Sıfır kontrolu için argüman sayacını bir düşür
jmp birsonraki_arguman
argumanlar_bitti:
mov eax, Mesaj
call dyaz
mov eax, edx ; Genel toplam ekrana yazdırılacak
call tyazAS
call son
; asmtutor05x.asm: Diğer programlara dahiledilecek altrutinler örneği.
;------------------------------------------
; int adant=atoi (Ascii'den tamsayıya çevrim fonksiyonu)
adant:
push ebx ; Argüman tamsayıya çevrilirken EBX, ECX, EDX, ESI önce yığına konur, fonksiyondan çıkarken de alınır
push ecx
push edx
push esi
mov esi, eax ; Argüman EAX'dan ESI kayıtçıya taşınır
mov eax, 0 ; Tamsayı argüman çevrim sonucu kayıtçısı
mov ecx, 0 ; Argümandaki tamsayı rakam adedi sayacı
.rakama_cevrim:
xor ebx, ebx ; EBX=0
mov bl, [esi+ecx] ; Aktüel ascii karakter=BL
cmp bl, 48
jl .bitir ; BL < ascii48 ise bitir
cmp bl, 57
jg .bitir ; BL > ascii57 ise bitir
sub bl, 48 ; ascii'den tamsayıyı soyutla
add eax, ebx ; EAX +=BL
mov ebx, 10
mul ebx ; EAX *=10
inc ecx ; Argümandaki rakam adedi sayacı
jmp .rakama_cevrim ; Argümanda rakam kalmayıncaya kadar döngüye devam
.bitir:
cmp ecx, 0
je .sonlan ; Argüman rakam adedi=0 ise başka işlem gerekmez
mov ebx, 10
div ebx ; EAX /=10 (son 10'lu katın iptali)
.sonlan:
pop esi ; EBX, ECX, EDX, ESI başlangıç değerlerine döner
pop edx
pop ecx
pop ebx
ret
;------------------------------------------
; void tyaz (Altsatırsız tamsayı yazdırma fonksiyonu)
tyaz:
push eax
push ecx
push edx
push esi
mov ecx, 0 ; Sayaç=0
tekrar_bol:
inc ecx ; Sayacı birartır
mov edx, 0
mov esi, 10 ; bölen=ESI=10
idiv esi ; EAX /=ESI (bölüm=EAX, kalan=EDX)
add edx, 48
push edx ; Kalanı yığına sakla
cmp eax, 0
jnz tekrar_bol ; EAX=0 değilse bölüm döngüsüne devam
tekrar_yaz:
dec ecx ; Sayacı birazalt
mov eax, esp ; Kalan EDX yığın adresini EAX'a taşı
call dyaz
pop eax ; yığındaki yazılan kalanı sil
cmp ecx, 0
jnz tekrar_yaz ; Sayaç=0 değilse dyaz döngüsüne devam
pop esi
pop edx
pop ecx
pop eax
ret
;------------------------------------------
; void tyazAS (Altsatırlı tamsayı yazdırma fonksiyonu)
tyazAS:
call tyaz
push eax
mov eax, 0Ah ; Altsatıra atlanacak
push eax
mov eax, esp
call dyaz
pop eax
pop eax
ret
;------------------------------------------
; int uzunluk (Mesaj dizgesi uzunluğunu hesaplayıp döndürür)
uzunluk:
push ebx
mov ebx, eax
birsonraki_karakter:
cmp byte [eax], 0
jz bitti
inc eax
jmp birsonraki_karakter
bitti:
sub eax, ebx
pop ebx
ret
;------------------------------------------
; void dyaz (Altsatırsız dizge yazdırma fonksiyonu)
dyaz:
push edx
push ecx
push ebx
push eax
call uzunluk
mov edx, eax
pop eax
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h
pop ebx
pop ecx
pop edx
ret
;------------------------------------------
; void dyazAS (Altsatırlı dizge yazdırma fonksiyonu)
dyazAS:
call dyaz ; Mesaj sonunda 0Ah yoksa sadece yazar, altsatıra geçmez
push eax
mov eax, 0Ah
push eax
mov eax, esp
call dyaz ; Sıfır uzunlukta dizge yazar ve altsatıra geçer
pop eax
pop eax
ret
;------------------------------------------
; void son (Programdan hatasız nihai çıkış)
son:
mov ebx, 0
mov eax, 1
int 80h
ret Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.
Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.
Every assembler may have it's own assembly language designed for a specific computers or an operating system.
Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
| Define Directive | Description | Allocated Space |
|---|---|---|
| DB | Define Byte | 1 byte |
| DW | Define Word | 2 bytes |
| DD | Define Doubleword | 4 bytes |
| DQ | Define Quadword | 8 bytes |
| DT | Define Ten Bytes | 10 bytes |
| Define Directive | Description |
|---|---|
| RESB | Reserve a Byte |
| RESW | Reserve a Word |
| RESD | Reserve a Doubleword |
| RESQ | Reserve a Quadword |
| REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
Loops are used to iterate a set of statements for a specific number of times.
mov ECX,n
L1:
;<loop body>
loop L1
where n specifies the no of times loops should iterate.
Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
procedure_name:
;procedure body
ret