如何在TASM中更改char中的位? [英] How to change bits in a char in TASM?
问题描述
我必须编写一个程序,该程序从文件读取字符,更改每个字符中的位,并将更改写入TASM中的新文件.
I have to write a program which is reading chars from a file, changing bits in every char and writing changes to a new file in TASM.
我编写了一个程序,该程序从文件读取字符并将它们写入新文件,但是我不知道如何更改字符中的位.
I've written a program which is reading chars from a file and writing them to a new file, but I don't know how to change bits in a char.
例如,这是我的包含chars的文件:
For example, here would be my file with chars:
a // 01100001
b // 01100010
c // 01100011
d // 01100100
因此,如果我们将第一位和第二位更改为1,则输出应为:
So, if we are changing first and second bit to 1, the output should be:
c // 01100011
c // 01100011
c // 01100011
g // 01100111
如何更改字符中的位
这是我的代码:
.model small
ASSUME CS:code, DS:data, SS:stack
stack segment word stack 'STACK'
dw 400h dup (00)
stack ends
data segment para public 'DATA'
ourFile:
dw 0FFFh
byteInFile:
db 00, 00, ' $'
handle:
dw ?
outputTextFile:
db 'TEXTOUT.CSV',0
inputTextFile:
db 'TEXT.CSV',0
data ends
writeToFile macro byte
push ax
push bx
push cx
push dx
mov ah, 40h
mov bx, word ptr[handle]
mov cx, 1
int 21h
pop dx
pop cx
pop bx
pop ax
endm
LOCALS @@
code segment para public 'CODE'
openFile proc near
push ax
push dx
mov ah, 3Dh
mov al, 00h
int 21h
jc @@end
mov word ptr ourFile, ax
@@end:
pop dx
pop ax
ret
openFile endp
closeFile proc near
push ax
push bx
mov ah, 3Eh
int 21h
@@end:
pop dx
pop ax
ret
closeFile endp
readLinesInFile proc near
push ax
push dx
push bx
push cx
push si
push di
mov si, dx
mov di, 0
@@repeat:
mov cx, 01
mov ah, 3Fh
int 21h
jc @@end
cmp ax, 00
je @@end
// here we have to change chars' bit?
// outputting chars
push ax
push dx
mov dl, byte ptr[si]
mov ah, 02h
int 21h
pop dx
pop ax
writeToFile byte ptr[si]
jmp @@repeat
@@end:
pop di
pop si
pop cx
pop bx
pop dx
pop ax
ret
readLinesInFile endp
begin:
mov ax, seg data
mov ds, ax
mov si, offset outputTextFile
mov cl, [ si ]
mov ch, 0
inc cx
add si, cx
mov al, 0
mov [ si ], al
; We create file
mov ah, 3ch
mov cx, 0
mov dx, offset outputTextFile
int 21h
; save handle
mov word ptr[handle], ax
; We open file
mov dx, offset inputTextFile
call openFile
mov bx, word ptr ourFile
mov dx, offset byteInFile
call readLinesInFile
; We close file
mov bx, word ptr ourFile
call closeFile
jmp @@Ok
mov ah, 3Eh
mov bx, word ptr[handle]
int 21h
@@Ok:
mov ah, 4ch
int 21h
code ends
end begin
推荐答案
您可以使用指令AND
将位设置为0,并使用指令OR
将位设置为1,例如:
You can use instruction AND
to set bits to 0, and instruction OR
to set bits to 1, examples :
BIT 7 BIT 0
▼ ▼
mov al, '9' ;◄■■ AL = 00111001 (57).
将最高位(7)设置为1,其他保持不变(使用OR
,0保持不变):
Set the highest bit (7) in 1 and leave the others unchanged (with OR
the 0s leave bits unchanged) :
BIT 7
▼ ▼
or al, 10000000b ;◄■■ AL = 10111001
现在将最低位(0)设置为0,其余的保持不变(使用AND
的1s保持不变):
Now set the lowest bit (0) in 0 and leave the others unchanged (with AND
the 1s leave bits unchanged) :
BIT 0
▼ ▼
and al, 11111110b ;◄■■ AL = 10111000
请注意每个指令与另一个指令是相反的,OR
设置为1s,AND
设置为0s,OR
使用掩码为0s,AND
使用掩码为1s.
Notice how each instruction is the opposite of the other, OR
to set 1s, AND
to set 0s, OR
uses a mask of 0s, AND
uses a mask of 1s.
您问如何一次更改第一(0)和第二(1)位:
You ask how to change first (0) and second (1) bits at a time :
▼▼
mov al, 'a' ;◄■■ AL = 01100001
or al, 00000011b ;◄■■ AL = 01100011
▲▲ ▲▲
同样,请注意OR
如何使用0的掩码使其他位保持不变.还要注意二进制数字末尾的"b".
Again, notice how OR
uses a mask of 0s to leave the other bits unchanged. Also notice the "b" at the end of the binary numbers.
最后,在您的代码中,您一次只从文件读取一个字节,该字节存储在变量byteInFile
中,因此:
Finally, in your code you are reading only one byte at a time from file, this byte is stored in variable byteInFile
, so :
// here we have to change chars' bit?
or byteInFile, 00000011b ;◄■■ SET BITS 0 AND 1, LEAVE THE REST UNCHANGED.
这篇关于如何在TASM中更改char中的位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!