asm 计算一个数(8bit)乘以2的n次方(n<128)
【摘要】
用汇编计算一个数(8bit)乘以2的n次方(n<128)
TITLE shifting_number
include irvine32.inc
.data
value dword 4 DUP(0)...
用汇编计算一个数(8bit)乘以2的n次方(n<128)
TITLE shifting_number
include irvine32.inc
.data
value dword 4 DUP(0) ;128位
n_2 byte 0 ;0-128
nt_2 byte 0 ;临时变量
result dword 8 DUP(0) ;256位
str0 byte "用移位做一个数乘以2的n次方",0
str1 byte "[该数由128位二进制数组成(已预设);2的次方数在0到128之间]",0
str2 byte "请输入2的次方数:",0
str3 byte "*2^",0
str4 byte "2的次方数输入有误导致结果溢出",0
bo byte 0 ;溢出标志
.code
input proc
;设置value的值
mov eax,12345678h
mov dword ptr value,eax
mov eax,0fea99321h
mov dword ptr value+4,eax
mov eax,12345678h
mov dword ptr value+8,eax
mov eax,0fea99321h
mov dword ptr value+12,eax
;==============
mov edx,offset str0
call writestring
call crlf
mov edx,offset str1
call writestring
call crlf
mov edx,offset str2
call writestring
call readint
mov n_2,al
mov nt_2,al
ret
input endp
;====================
calculate proc
mov al,n_2
;=====================
cmp al,32 ;大于32位时跳后
ja NEXT_1
;=============================
mov ebx,dword ptr value+12
mov edx,0 ;每次使用时都将edx清零
mov cl,nt_2
shld edx,ebx,cl ;每次edx都"不会被"清0后再进行移动,且每次移动后"后面的寄存器"值不变
shl ebx,cl
add dword ptr result+16,edx
add dword ptr result+12,ebx
mov ebx,dword ptr value+8
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+12,edx
add dword ptr result+8,ebx
mov ebx,dword ptr value+4
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+8,edx
add dword ptr result+4,ebx
mov ebx,dword ptr value
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+4,edx
add dword ptr result,ebx
;================================
jmp OVER
NEXT_1:
cmp al,64 ;大于64跳
ja NEXT_2
mov bl,32
sub nt_2,bl
;===============================
mov ebx,dword ptr value+12
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+20,edx
add dword ptr result+16,ebx
mov ebx,dword ptr value+8
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+16,edx
add dword ptr result+12,ebx
mov ebx,dword ptr value+4
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+12,edx
add dword ptr result+8,ebx
mov ebx,dword ptr value
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+8,edx
add dword ptr result+4,ebx
;===============================
jmp OVER
NEXT_2:
cmp al,96 ;大于96跳
ja NEXT_3
mov bl,64
sub nt_2,bl
;================================
mov ebx,dword ptr value+12
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+24,edx
add dword ptr result+20,ebx
mov ebx,dword ptr value+8
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+20,edx
add dword ptr result+16,ebx
mov ebx,dword ptr value+4
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+16,edx
add dword ptr result+12,ebx
mov ebx,dword ptr value
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+12,edx
add dword ptr result+8,ebx
;===========================
jmp OVER
NEXT_3:
cmp al,128 ;大于128跳
jae NEXT_4
mov bl,96
sub nt_2,bl
;===========================
mov ebx,dword ptr value+12
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+28,edx
add dword ptr result+24,ebx
mov ebx,dword ptr value+8
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+24,edx
add dword ptr result+20,ebx
mov ebx,dword ptr value+4
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+20,edx
add dword ptr result+16,ebx
mov ebx,dword ptr value
mov edx,0
mov cl,nt_2
shld edx,ebx,cl
shl ebx,cl
add dword ptr result+16,edx
add dword ptr result+12,ebx
;============================
jmp OVER
NEXT_4:
cmp al,128 ;等于128跳
jne NEXT_5
mov eax,dword ptr value+12
mov dword ptr result+28,eax
mov eax,dword ptr value+8
mov dword ptr result+24,eax
mov eax,dword ptr value+4
mov dword ptr result+20,eax
mov eax,dword ptr value
mov dword ptr result+16,eax
;===========================
jmp OVER
NEXT_5:
;========================
mov bl,1 ;溢出标志为1,表示输入有误
mov bo,bl
OVER:
ret
calculate endp
;====================
output proc;输出函数
mov bl,bo
cmp bl,0
jnz NEXT2_1
mov eax,dword ptr value+12
call writeHex
mov eax,dword ptr value+8
call writeHex
mov eax,dword ptr value+4
call writeHex
mov eax,dword ptr value
call writeHex
mov edx,offset str3
call writestring
movzx eax,n_2
call writedec
mov al,"="
call writechar
call crlf
mov eax,dword ptr result+28
call writeHex
mov eax,dword ptr result+24
call writeHex
mov eax,dword ptr result+20
call writeHex
mov eax,dword ptr result+16
call writeHex
mov eax,dword ptr result+12
call writeHex
mov eax,dword ptr result+8
call writeHex
mov eax,dword ptr result+4
call writeHex
mov eax,dword ptr result
call writeHex
jmp NEXT2_2
NEXT2_1:
mov edx,offset str4
call writestring
NEXT2_2:
call crlf
call readchar
ret
output endp
;====================
main proc
call input
call calculate
call output
exit
main endp
end main
- 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
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/52145355
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)