Lab2023 4 Arithmetic Instructions
Lab2023 4 Arithmetic Instructions
2. Prerequest
- review the theory of assembly language
3. Tools
- Computer with installed softwares
4. Duration
- 4 hours
5. Contents:
5.1. Introduction
Addition, Increment, Add-with-carry and Exchange-and-add:Contents of the rightmost 8 bits of
the FLAGS register can change (+ Overflow) for arithmetic and logic instructions
Flags include:
✓ Z (result zero)
✓ C (carry out)
✓ A (half carry out)
✓ S (result positive)
✓ P (result has even parity)
✓ O (overflow occurred)
Instruction
Addition Add add <dest>, <src>
Inc inc <operand>
Adc adc <dest>, <src>
Xadd
Substraction Sub sub <dest>, <src>
Dec dec <operand>
Sbb
Multiplication Mul mu1 <src>
Imul imul <source>
Division Div div <src>
Idiv idiv <src>
6. Examples
6.1. Add example 1 (lab41.nasm)
segment .data
a: dq 200
b: dq 100
add: dw "Addition:",0
Pages - 1
Computer architecture labs
fmt: dq "%s %lld ",10,0
segment .text
global _start
extern printf
_start:
push RBP
mov RAX, 0
mov RDX, 0
mov RAX, [a]
add RAX, [b]
mov RDI, fmt
mov RSI, add
mov RDX, RAX
mov RAX, 0
call printf
mov RAX,60
syscall
pop RBP
ret
SYS_EXIT equ 60
section .data
data dd 40
section .text
global _start
_start:
nop
mov eax,0
mov ebx,0
mov ecx,0
mov al,20
add al,10
movsx eax,al
mov cx,100
Pages - 2
Computer architecture labs
add bx,cx
movsx ebx,bx
mov edx,100
add edx,edx
add eax,[data]
add [data],eax
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
✓ nasm -o lab42.o -felf64 lab42.nasm
✓ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o lab42 lab42.o -lc
✓ ./lab42
✓ Edit program to see result
✓ Using debug to verify each instruction and result
6.3. Example 3: Add test2 (lab43.nasm)
;Addtest2.asm
;;; An example of the ADD instruction and negative numbers
SYS_EXIT equ 60
section .data
data dd -40
section .text
global _start
_start:
nop
mov eax,-10
mov ebx,-200
mov ecx,80
add eax,[data]
add eax,ecx
add eax,ebx
add [data],eax
add dword [data],210
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
✓ nasm -o lab43.o -felf64 lab43.nasm
✓ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o lab43 lab43.o -lc
✓ Edit program to see result
✓ Using debug to verify each instruction and result
6.4. Example 4: Add test3 (lab44.nsam)
;Addtest3.asm
Pages - 3
Computer architecture labs
;;; An example of detecting a carry condition
SYS_EXIT equ 60
section .text
global _start
_start:
nop
mov ebx,0
mov bl,190
mov al,100
add bl,al
jc over
mov eax,SYS_EXIT
syscall
over:
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
✓
6.5. Example 5: Adc test (lab45.nasm)
;Adctest.asm
;;; An example of using the ADC instruction
section .data
data1 dq 7252051615
data2 dq 5732348928
output db `The result is %qd\n`,0
section .text
global _start
extern exit,printf
_start:
mov ebx,[data1]
mov eax,[data1+4]
mov edx,[data2]
mov ecx,[data2+4]
add edx,ebx
adc ecx,eax
shl rcx,32
add rcx,rdx
mov rsi,rcx
mov rdi,output
mov rax,0
call printf
add rsp,24
mov rdi,0
Pages - 4
Computer architecture labs
call exit
;;; *EOF*
✓ Compile and run program
✓ nasm -o lab45.o -felf64 lab45.nasm
✓ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o lab45 lab45.o -lc
✓ ./lab45
✓ The result is 12984400543
6.6. Example 6: CPIid test (lab46.nasm)
;Cpuidtest.asm
;;; An example of using the TEST instruction
section .data
output_cpuid db `This processor supports the CPUID
instruction\n`,0
output_nocpuid db `This processor does not support the CPUID
instruction\n`,0
section .text
global _start
extern exit,printf
_start:
nop
pushf
pop rax
mov rdx,rax
xor rax,0x00200000
push rax
popf
pushf
pop rax
xor rax,rdx
test rax,0x00200000
jnz cpuid
mov rdi,output_nocpuid
mov rax,0
call printf
mov rdi,0
call exit
cpuid:
mov rdi,output_cpuid
mov rax,0
call printf
mov rdi,0
call exit
;;; *EOF*
✓ Compile and run program
Pages - 5
Computer architecture labs
✓ nasm -o lab46.o -felf64 lab46.nasm
✓ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o lab46 lab46.o -lc
✓ ./lab46
✓ This processor supports the CPUID instruction
6.7. Example 7: Subtest1
Subtest1.asm
;;; An example of the SUB instruction
SYS_EXIT equ 60
section .data
data dd 40
section .text
global _start
_start:
nop
mov eax,0
mov ebx,0
mov ecx,0
mov al,20
sub al,10
movsx eax,al
mov cx,100
sub bx,cx
movsx ebx,bx
mov edx,100
sub edx,eax
sub eax,[data]
sub [data],eax
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
6.8. Example 8: Subtest2
Subtest2.asm
;;; An example of a subtraction carry
SYS_EXIT equ 60
section .text
global _start
_start:
nop
mov eax,5
mov edi,2
sub edi,eax
jc under
mov eax,SYS_EXIT
syscall
Pages - 6
Computer architecture labs
under:
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
6.9. Example 9: Subtest3
Subtest3.asm
;;; An example of an overflow condition in a SUB instruction
section .data
output db `The result is %d\n`,0
section .text
global _start
extern printf,exit
_start:
mov esi,-1590876934
mov eax,1259230143
sub esi,eax
jo over
mov rdi,output
mov rax,0
call printf
add rsp,8
mov rdi,0
call exit
over:
mov rsi,0
mov rdi,output
mov rax,0
call printf
mov rdi,0
call exit
;;; *EOF*
✓ Compile and run program
6.10. Example 10: Sbbtest1
Sbbtest.asm
;;; An example of using the SBB instruction
section .data
data1 dq 7252051615
data2 dq 5732348928
output db `The result is %qd\n`,0
section .text
global _start
extern exit,printf
_start:
Pages - 7
Computer architecture labs
nop
mov ebx,[data1]
mov eax,[data1+4]
mov edx,[data2]
mov ecx,[data2+4]
sub edx,ebx
sbb ecx,eax
shl rcx,32
add rcx,rdx
mov rsi,rcx
mov rdi,output
mov rax,0
call printf
add rsp,8
mov rdi,0
call exit
;;; *EOF*
✓ Compile and run program
6.11. Example 11: Mult example
segment .data
a: dq 200
b: dq 100
mul: dw "Multiplication:",0
fmt: dq "%s %lld ",10,0
segment .text
global main
extern printf
main:
push RBP
mov RAX , 0
mov RDX, 0
mov RAX , [a]
mov RDX , [b]
mul RDX
mov RDI, fmt
mov RSI, mul
mov RDX, RAX
mov RAX , 0
call printf
mov RAX, 0
pop RBP
ret
✓ Compile and run program
6.12. Example 12: Multiplication scan
segment .data
a: dq 2
b: dq -1
mul: dw "Multiplication:",0
Pages - 8
Computer architecture labs
fmt: dq "%s %lld ",10,0
fmt_in: dq "%lld", 0
segment .text
global main
extern printf
extern scanf
main:
push RBP
mov RAX, 0
mov RSI, a
mov RDI, fmt_in
call scanf
mov RAX, 0
mov RSI, b
mov RDI, fmt_in
call scanf
mov RAX , 0
mov RDX, 0
mov RAX , [a]
mov RDX , [b]
mul RDX
mov RDI, fmt
mov RSI, mul
mov RDX, RAX
mov RAX , 0
call printf
mov RAX, 0
pop RBP
ret
✓ Compile and run program
6.13. Example 13: Multest1
Multest.asm
;;; An example of using the MUL instruction
SYS_EXIT equ 60
section .data
data1 dd 315814
data2 dd 165432
result dq 0
output db `The result is %qd\n`,0
section .text
global _start
extern exit,printf
_start:
nop
mov eax,[data1]
mul dword [data2]
mov [result],eax
mov [result + 4],edx
mov rsi,qword [result]
Pages - 9
Computer architecture labs
mov rdi,output
mov rax,0
call printf
mov eax,SYS_EXIT
mov rdi,0
syscall
6.14. Example 14: Imultest 1
Imulttest.asm
;;; An example of the IMUL instruction formats
SYS_EXIT equ 60
section .data
value1 dd 10
value2 dd -35
value3 dd 400
section .text
global _start
_start:
nop
mov ebx,[value1]
mov ecx,[value2]
imul ecx, ebx
mov edx,[value3]
imul eax, edx, 2
mov eax,SYS_EXIT
mov rdi,0
syscall
;;; *EOF*
✓ Compile and run program
6.15. Example 15: iMultest2
Imultest2.asm
;;; An example of detecting an IMUL overflow
SYS_EXIT equ 60
section .text
global _start
_start:
nop
mov ax,680
mov cx,100
imul cx
jo over
mov eax,SYS_EXIT
mov rdi,0
syscall
over:
mov eax,SYS_EXIT
mov rdi,1
Pages - 10
Computer architecture labs
syscall
;;; *EOF*
✓ Compile and run program
6.16. Example 16: divison
segment .data
a: dq 200
b: dq 100
div: dw "Quotient:",0
fmt: dq "%s %lld ",10,0
segment .text
global main
extern printf
main:
push RBP
mov RAX , 0
mov RBX , 0
mov RDX, 0
mov RAX , [a]
mov RBX , [b]
div RBX
mov RDI, fmt
mov RSI, div
mov RDX, RAX
mov RAX , 0
call printf
mov RAX, 0
pop RBP
ret
✓ Compile and run program
;nasm -f elf64 division.asm && gcc division.o -o division &&
./division
section .text
global _start
extern exit,printf
_start:
nop
mov eax,[dividend]
mov edx,[dividend + 4]
div dword [divisor]
mov [quotient],eax
mov [remainder],edx
mov esi,eax
mov rdi,output
mov rax,0
call printf
mov rdi,0
call exit
Pages - 12
Computer architecture labs
;;; *EOF*
✓ Compile and run program
6.19.
Pages - 13
Computer architecture labs