You are on page 1of 6

ASSEMBLY

Beberapa perintah assembly sederhana:

MOV BX,AX ; This copies the contents of the AX register into the BX register.
MOV CH,DH ; This copies the top byte of the DX register into the top byte of the CX register.
MOV BH,DL ; This copies the bottom byte of the DX register into the top byte of BX.
MOV AH,12 ; This puts the value 12 decimal into the top half of the AX register.
MOV AH,0Ch ; This does the same as the above except that the number is given in hexadecimal.
; Hexadecimal numbers MUST begin with a digit and end with a "h".
MOV DL,"*" ; This puts the character "*" into DL (lower half of DX).
MOV DL,42 ; This does the same thing, as characters are stored as numbers. ( ASCII char 42 = "*" )

mov ah, 1 ;read character from standard input, with echo, result is stored in AL.
mov ah, 2 ; write character to standard output; DL = character to write
int 21h ; if there is no character in the keyboard buffer, the function waits until any key is pressed.

mov ah, 9 ; output of a string at DS:DX. String must be terminated by '$'


contoh:
org 100h
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "hello world $"

mov ah, 0ah ; input of a string to DS:DX, fist byte is buffer size, second byte is number of chars actually read
move ah, 0Ch ;flush keyboard buffer and read standard input.

MOV AH,04Ch ; return control to the operating system (stop program)


INT 21h ; Call the interrupt to exit intrupt 21

mov ah,00
int 16h ;wait for keypress
Logic instructions
 LOGIC tujuan, source
AND ax, dx ; ax AND dx  hasil disimpan di ax
OR ax, bx ; ax OR bx  hasil disimpan di ax
XOR bx, ax ; bx XOR ax  hasil disimpan dib bx

Arithmetic instructions
ADD tujuan, source  ADD ax, bx ; ax + bx simpan di ax
SUB tujuan, source  SUB ax, bx ; ax – bx simpan di ax
ADD cx,num ;add num value to cx value
SUB num,5 ;subtract 5 from variable num
DIV source  DIV SI ; div ax dengan nilai SI
div bl ;bagi ax dengan nilai di bl. Hasilnya simpan di al, sisanya di ah
div ch ;al = ax / ch, ah = ax % ch (% = modulus operator in C++ or "mod" in Pascal)
div 7 ;al = ax / 7, ah = ax % 7
IMUL ax,10 ; kalikan ax dengan 10 simpan di ax
mul bl ;multiply bl * al giving result in ax.
mul b[bx] ;ax = value-pointed-to-by-bx * al

Increment and Decrement


inc ax ;add one to contents of ax register  sama seperti i++
dec bx ;subtract one from bx register  sama seperti i—
dec b[bx] ;subtract one from byte pointed to by bx register
inc var_name ;increment the variable var_name

Comparison instructions (bandingkan)


CMP destiny, source  CMP bil1, bil2 ; apakah bil2 sama dengan bil1

Loop Instruction
LOOP label ; kurangi nilai cx dengan 1 jika belum 0 jalankan lagi perintah2 dari label

Jump Instruction
JMP label ; lompat ke label
JE label ; jump if equal / lompat ke label jika sama
JNE label ; jump if not equal / lompat ke label jika tidak sama
JG label ; jump if greater
JL label ; jump if lower
JGE label ; jump if greater or equal / JNL label ; jump if not lower
JNZ label ; jump if not Zero / lompat ke label jika tidak kosong
1. ADD2BIL
; === Makro ===
%macro printstr 1 ; cetak tulisan ke layar org 100h
jmp %%cetak
%%str db 13,10,%1,'$'
start:
%%cetak:
mov dx, %%str printstr 'Menambahkan 2 buah bilangan
mov ah, 9 bulat'
int 21h ; int 21h = readln Tampilkan
printstr '(Maksimum hasilnya 65535)' langsung ke
%endmacro
printstr '---------------------------------' layar
%macro printvar 1 ; cetak variable ke layar printstr ' '
mov dx, %1
Baca inputan
mov ah, 9 printstr 'Tulis Bilangan 1: ' keyboard
int 21h
%endmacro
readstr sbil1 masukkan ke
variable sbil1
%macro readstr 1 ; baca inputan keyboard printstr 'Tulis Bilangan 2: '
masukkan ke
mov byte [%1], 10 readstr sbil2 variable sbil2
mov dx, %1
mov ah, 0Ah
int 21h str2bin sbil1, nbil1 Ubah ke
str2bin sbil2, nbil2 binner
%endmacro

%macro str2bin 2 ; rubah string ke binner mov ax, [nbil1]


mov CL, byte [%1+1] Masukan ke register ax nbil1
xor ax,ax
add ax, [nbil2] Tambahkan ke register ax nbil2
xor dx,dx mov [hasil], ax nilai ax dimasukkan ke hasil
mov BX, %1+2
%%ulangi: Ubah hasil ke
bil2str hasil, strhasil
IMUL AX,10 string
MOV DL, byte [bx]
AND DL, 0Fh printstr 'Jumlahnya adalah: '
Tampilkan hasil
ADD AX, DX printvar strhasil
(variable strhasil)
INC BX printstr ' '
LOOP %%ulangi
MOV [%2], AX
%endmacro
mov ah,4ch
int 21h
%macro bil2str 2 ; ubah nilai ke string
MOV BX, %2 sbil1: times 10 db '0'
%%terus:
sbil2: times 10 db '0'
inc BX
cmp byte[BX],'$'
jne %%terus nbil1: dw 0
dec BX nbil2: dw 0
MOV AX, [%1]
MOV SI,10
%%lagi:
hasil: dw 0
SUB DX,DX strhasil: times 6 db ' '
DIV SI db '$'
ADD DX,'0'
MOV [BX], DL
end
DEC BX
OR AX,AX
JNZ %%lagi
%endmacro

; ===Program utama ===

2. TAMBAH
; ==== makro === org 100h
start:
%macro printstr 1
jmp %%cetak printstr 'Menambahkan 2 buah bilangan bulat'
%%str db 13,10,%1,'$' jmp %%cetak
%%cetak:
%%str db 13,10,%1,'$'
mov dx, %%str
mov ah, 9
int 21h %%cetak:
%endmacro mov dx, %%str
mov ah, 9
%macro printvar 1 int 21h
mov dx, %1
printstr '(Maksimum hasilnya 65535)'
mov ah, 9
int 21h jmp %%cetak
%endmacro %%str db 13,10,%1,'$'

%macro readstr 1 %%cetak:


mov byte [%1], 10 mov dx, %%str
mov dx, %1
mov ah, 9
mov ah, 0Ah
int 21h int 21h
%endmacro printstr '---------------------------------'
jmp %%cetak
%macro str2bin 2 %%str db 13,10,%1,'$'
mov CL, byte [%1+1]
xor ax,ax
%%cetak:
xor dx,dx
mov BX, %1+2 mov dx, %%str
%%ulangi: mov ah, 9
IMUL AX,10 int 21h
MOV DL, byte [bx] printstr ' '
AND DL, 0Fh jmp %%cetak
ADD AX, DX
%%str db 13,10,%1,'$'
INC BX
LOOP %%ulangi %%cetak:
MOV [%2], AX mov dx, %%str
%endmacro mov ah, 9
int 21h

%macro bil2str 2 printstr 'Tulis Bilangan 1: '


MOV BX, %2
%%terus:
jmp %%cetak
inc BX %%str db 13,10,%1,'$'
cmp byte[BX],'$'
jne %%terus %%cetak:
dec BX mov dx, %%str
MOV AX, [%1] mov ah, 9
MOV SI,10
%%lagi:
int 21h
SUB DX,DX readstr sbil1
DIV SI mov byte [%1], 10
ADD DX,'0' mov dx, %1
MOV [BX], DL mov ah, 0Ah
DEC BX int 21h
OR AX,AX
JNZ %%lagi
%endmacro
printstr 'Tulis Bilangan 2: '
; === program utama ==== jmp %%cetak
%%str db 13,10,%1,'$'
SUB DX,DX
%%cetak: DIV SI
mov dx, %%str ADD DX,'0'
mov ah, 9 MOV [BX], DL
int 21h DEC BX
readstr sbil2 OR AX,AX
mov byte [%1], 10 JNZ %%lagi
mov dx, %1
mov ah, 0Ah printstr 'Jumlahnya adalah: '
int 21h jmp %%cetak
%%str db 13,10,%1,'$'
str2bin sbil1, nbil1
mov CL, byte [%1+1]
xor ax,ax %%cetak:
xor dx,dx mov dx, %%str
mov BX, %1+2 mov ah, 9
%%ulangi: int 21h
IMUL AX,10 printvar strhasil
MOV DL, byte [bx] mov dx, %1
AND DL, 0Fh mov ah, 9
ADD AX, DX int 21h
INC BX printstr ' '
LOOP %%ulangi jmp %%cetak
MOV [%2], AX %%str db 13,10,%1,'$'
str2bin sbil2, nbil2 %%cetak:
mov CL, byte [%1+1] mov dx, %%str
xor ax,ax mov ah, 9
xor dx,dx int 21h
mov BX, %1+2
%%ulangi: mov ah,4ch
IMUL AX,10 int 21h
MOV DL, byte [bx]
AND DL, 0Fh sbil1: times 10 db '0'
ADD AX, DX sbil2: times 10 db '0'
INC BX
LOOP %%ulangi nbil1: dw 0
MOV [%2], AX nbil2: dw 0

mov ax, [nbil1] hasil: dw 0


add ax, [nbil2] strhasil: times 6 db ' '
mov [hasil], ax db '$'

bil2str hasil, strhasil end


MOV BX, %2
%%terus:
inc BX
cmp byte[BX],'$'
jne %%terus
dec BX

MOV AX, [%1]


MOV SI,10
%%lagi:

You might also like