-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtwos_complement.asm
More file actions
65 lines (57 loc) · 2.52 KB
/
twos_complement.asm
File metadata and controls
65 lines (57 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
; =============================================================================
; TITLE: Two's Complement (Negation)
; DESCRIPTION: Demonstrate how to negate a signed 16-bit integer using
; the Two's Complement arithmetic method.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
NUM DW 25 ; Positive number
NEG_NUM DW ? ; Expected: -25 (in Two's Comp)
MSG DB 'Negation completed successfully.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; Environment Setup
MOV AX, @DATA
MOV DS, AX
; -------------------------------------------------------------------------
; THE 'NEG' INSTRUCTION
; This is the standard 8086 instruction to find Two's Complement.
; Operation: Dest = (NOT Dest) + 1
; -------------------------------------------------------------------------
MOV AX, NUM
NEG AX ; Atomically perform 2's complement
MOV NEG_NUM, AX ; Store result
; -------------------------------------------------------------------------
; MANUAL ALTERNATIVE (Same result):
; MOV AX, NUM
; NOT AX ; Perform One's Complement (flip bits)
; INC AX ; Add 1
; -------------------------------------------------------------------------
; Output status message
LEA DX, MSG
MOV AH, 09H
INT 21H
; Program termination
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. TWO'S COMPLEMENT:
; - In Two's Complement, the MSB acts as the sign bit (1 for Negative).
; - Range for 16-bit signed: -32,768 to +32,767.
; - Value +25 in Hex: 0019h
; - Value -25 in Hex: FF E7h (NOT 0019h = FF E6h, +1 = FF E7h)
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =