-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwosComplementShortcut.asm
78 lines (59 loc) · 1.78 KB
/
TwosComplementShortcut.asm
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
66
67
68
69
70
71
72
73
74
75
76
77
78
TITLE Twos_Complement_Shortcut (Chapter 7) (TwosComplementShortcut.asm)
COMMENT !
Student: Rachel Nguyen
Class: CSCI 241
Instructor: Ding
Assignment: Ch07, Twos_Complement_Shortcut
Due Date: 3/18/21
Description:
This program converts a number to its two's complement
without forming its ones' complement and plus one.
!
INCLUDE Irvine32.inc
.data
msgUserInput BYTE "Enter a 32-bit signed integer in decimal: ", 0
msgBinary BYTE "Your integer input in binary is ", 0
msgBinResult BYTE "The 2's complement in binary is ", 0
msgDecResult BYTE "The 2's complement in decimal is ", 0
.code
main PROC
mov edx, OFFSET msgUserInput ; prompt user for signed decimal input
call WriteString
call ReadInt
mov edx, OFFSET msgBinary ; show user input in binary
call WriteString
call WriteBin
call crlf
call TwosCompShortcut ; convert binary input into its two's complement
mov edx, OFFSET msgBinResult ; show two's complement in binary
call WriteString
call WriteBin
call crlf
mov edx, OFFSET msgDecResult ; show two's complement in decimal
call WriteString
call WriteInt
call crlf
exit
main ENDP
;---------------------------------------------------------------
TwosCompShortcut PROC
; Description: Converts a number to its two's complement without
; forming its ones' complement and plus one.
; Recieves: EAX as a signed integer.
; Returns: EAX the 2's complement of EAX.
;---------------------------------------------------------------
mov ebx, 1
L1:
test eax, ebx ; start at LSB and check for 1
jnz next ; if 1 found, jump
shl ebx, 1 ; else, move to next bit
jnc L1
next:
shl ebx, 1 ; shift left to keep first 1 before XORing
L2:
XOR eax, ebx ; flip remaining bits
shl ebx, 1
jnz L2
ret
TwosCompShortcut ENDP
END main