Re: referring to segments other than DS - how?



On Wed, 27 Feb 2008 13:21:51 -0600, Pop Tart wrote:

On Wed, 27 Feb 2008 17:48:51 +0000, Frank Kotler wrote:

Using 16-bit instructions, si and di are "index" registers, bx and bp
are "base" registers. You can use an (optional) offset, plus an
(optional) base register, plus an (optional) index register. That is,
[si + bx] is okay but [si + di] is not. We can, even in 16-bit code,
use 32-bit addressing modes - mov al, BYTE PTR es:[edx] would work. I
think you'd need to "enable" 32-bit instructions for Masm - ".386"
*after* the ".model" directive, IIRC. Simplest thing is to stick with
si, di, and bx for addressing (bp is "special" in that it defaults to
ss:[bp]).

Best,
Frank

Thanks Frank. Maybe you or someone else can help with this. Assuming I
only have 16-bit registers, and I've got a buffer in BSS, how do I write
the buffer to a file? If I understand correctly, function 40h / int 21h
needs the address of the buffer to be in DS:DX, so I have to move the
BSS segment to DS. However, my program is terminating on the int 21h
below - before even setting error code to AX or setting the Carry Flag.

(I have ES set to BSS below. label 'InputBuffer is an offset into BSS)

mov ax, 40h ; func. write array
mov bx, EncFileHandle ; file handle to bx mov cx,
500 ; bytes to
write from 3fh abv

mov di, es
mov ds, di ; set ds to es for inp.buffer

mov dx,OFFSET InputBuffer
int 21h ;terminates abrubtly here

cmp ax,0 ; did we write anything
je EmptyOutput

Only thing I can think is that EncFileHandle, which is a label to an
offset into the regular Data Segment (not BSS), causes it to screw up...
However, reading a file into this array in the same fashion, with 3Fh /
int 21h, didn't have a problem. If the function *is* interpreting
EncFileHandle to be in *BSS*, how the heck does anyone write to an
uninitialized buffer? !@#$%! :)

Again, I'm trying not to use 32-bit registers. Many thanks in advance.

arrghh,,, can't figure it out.

Here's the complete code if anyone is inclined to assemble and see if you
stop on the int 21h / function 40h (write array to file).

I put all the labels in the regular initialized data section, so as to
avoid any unnecessary complications with BSS. I'm still getting abrubt
stop on function 40h - line 160.

this is masm 6.11 with the 16 bit linker:
ml /c /Zi main.asm
link main.obj,,,,/co

That addes symbolic debug info on assembly, and codeview debugging info
when linking.

;[[[[[[[[[[[[[[[[[[[BEGIN MAIN.ASM]]]]]]]]]]]]]]]]]]]]]]

TITLE ENCRYPT (main.asm)
..MODEL small
..STACK 100h
..286

COUNT equ 500
..data

OpenFileName BYTE 128 DUP(0)
EncFileName BYTE "enc.txt"
;OpenFileHandle WORD ?
;EncFileHandle WORD ?
;;;; data bss was here .data?
InputBuffer BYTE COUNT DUP(?)
OpenFileHandle WORD ?
EncFileHandle WORD ?

..code
main PROC
mov ax, @data
mov ds, ax

mov dx, OFFSET OpenFileName
call GetCommandTail

mov si, OFFSET OpenFileName
call OpenFile ;use openFileName, for read

mov si, OFFSET EncFileName
call OpenEncFile ;for writing out encrypted

call EncryptFile

mov ah, 4ch
mov al, 0
int 21h
main ENDP
;********************************************************************
;////////////////////////////////////////////////////////////////////
;********************************************************************
OpenEncFile PROC
;---------------
;rcvs: si = offset of filename to write
;rtns:
;uses:
;desc:
;
;--------------------------------------------------------------------
pusha
mov ax, 716Ch ;extended open/create
mov bx, 2 ;write file
mov cx, 0 ;normal attrib
mov dx, 10h + 02h ;create or truncate exist.
; ds:si was passed in to this proc
int 21h ;will want to change this int
;doesn't filter 0Dh, 0Ah

jc ReturnError ;check for errors opening
mov EncFileHandle, ax ;save handle
jmp Exit

ReturnError:
call WriteError ;ax=error code

Exit:
popa
ret
OpenEncFile ENDP
;********************************************************************
;////////////////////////////////////////////////////////////////////
;********************************************************************
OpenFile PROC
;------------
;rcvs: si = offset of filename to read;
;rtns:
;uses:
;desc:
;
;--------------------------------------------------------------------
pusha
mov ax, 716Ch ;extended open/create
mov bx, 0 ;read file
mov cx, 0 ;normal attrib
mov dx, 1 ;open only, do not create

;ds:si was passed in to this proc
int 21h ;will want to change this int
;doesn't filter 0Dh, 0Ah

jc ReturnError ;check for errors opening
mov OpenFileHandle, ax ;save handle
jmp Exit
ReturnError:
;ax=error code
call WriteError

Exit:
popa
ret

OpenFile ENDP
;********************************************************************
;////////////////////////////////////////////////////////////////////
;********************************************************************
EncryptFile PROC
;---------------
;rcvs:
;rtns:
;uses: OpenFileHandle, EncFileHandle
;desc: reads one file, writes encrypted data to another file
; closes files when done
;future*: use heap alloc buffer when I learn how to do that.
;future*: fix calls to WriteError
;--------------------------------------------------------------------
XORVAL equ 0EFh
pusha
mov ah,3Fh ; read array of bytes
mov bx, OpenFileHandle ; src file handle
mov cx, COUNT ; max bytes to read
mov dx, OFFSET InputBuffer ;
int 21h ; AX = bytes read

push ax

; close the OpenFile (read file)
mov ah, 3Eh ; close func.
mov bx, OpenFileHandle
int 21h

;now encrypt the buffer up to the size returned (size = ax)

pop ax ; ax= bytes read from above
push ax ; save again for writing
mov cx, ax ; set loop w/ bytes read

mov di, OFFSET InputBuffer
L1:
mov al, BYTE PTR [di]
xor al, XORVAL
mov BYTE PTR [di], al
inc di
loop L1

;now write the encrypted buffer to the new file
; first set the file pointer
mov ah, 42h ; funct to set file ptr
mov al, 0 ; methd= off from beginng
mov bx, EncFileHandle ;
mov cx, 0
mov dx, 0
int 21h ; check CF=0 here for ok
; DX:AX is new ptr offset

pop ax

mov cx, ax ; bytes to write from 3fh abv
mov bx, EncFileHandle ; file handle to bx
mov ax, 40h ; func. write array
mov dx, OFFSET InputBuffer
int 21h

Exit:
mov ax, @data ; reset DS to data seg.
mov ds, ax
popa
ret
EncryptFile ENDP
;********************************************************************
;////////////////////////////////////////////////////////////////////
;********************************************************************
GetCommandTail PROC
;------------------
;rcvs: di=pointer to buffer to rcv file name
;rtns: cf=1 if failure, else cf=0
;uses: es=psp
;desc: reads the command tail looking for a file name to pass to
; caller
; 1) get psp; 2)get count of command tail, set cf=1 & exit if 0
; 3) scan out spaces, if only spaces, set cf=1 & exit
; 4)
;--------------------------------------------------------------------
SPACE=20h
push es
pusha

mov ah,62h
int 21h ;rtrns bx = offset to process psp
mov es,bx ;store psp to es

mov si, dx ;offset to buffer rcvg command tail
mov di, 81h ;posit. of space in cmd tail
mov cx, 0 ;init counter
mov cl, es:[di-1] ;count is first pos of cmd tail
cmp cx, 0 ;empty cmd tail?
je L2 ;if 0 count, jump out of proc

cld ;dir.flag set forward
mov al, SPACE
repz scasb ;scas uses es:di
jz L2 ;
dec di ;di always one beyond
inc cx
L1: mov al, es:[di]
mov [si],al ;pointed to by ds:si
inc si
inc di
loop L1
clc ; cf=0 means tail found
jmp L3
L2: stc ; cf=1 means no cmd tail found
L3: mov byte ptr [si], 0 ;store null byte
popa
pop es

ret
GetCommandTail ENDP
;********************************************************************
;////////////////////////////////////////////////////////////////////
;********************************************************************
WriteError PROC
;--------------
;rcvs: ax = error code
;rtns:
;uses:
;desc:
;
;--------------------------------------------------------------------
pusha

popa
ret
WriteError ENDP
;********************************************************************
;||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
END main


;[[[[[[[[[[[[[[[[[[[[END MAIN.ASM]]]]]]]]]]]]]]]]]]]]]]]

--
Regards,
Pop Tart
.



Relevant Pages

  • Re: referring to segments other than DS - how?
    ... only have 16-bit registers, and I've got a buffer in BSS, how do I write ... needs the address of the buffer to be in DS:DX, so I have to move the BSS ... EncFileHandle; file handle to bx ... I'm trying not to use 32-bit registers. ...
    (alt.lang.asm)
  • [EXPL] KCMS Configure Tool Security Vulnerability (Exploit)
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... A command line buffer overflow is easy to control as long as ... * registers being set, and we're overwriting the saved registers ... * of overwriting the saved i7 register, we overwrite ...
    (Securiteam)
  • Re: Geriatric Pentium
    ... processor scavenging spare ram cycles to back it up to ram and restore ... Let's say I had hardware for 128 registers. ... instituted a squirrel cage circular buffer of hardware registers. ... Ditto for caching the stack. ...
    (comp.lang.java.advocacy)
  • Re: Bit manipulation
    ... >>> How can the bit stream can rotated right in C programs ... >>> to rotate the bitstream exactly by n position, ... a one bit delay buffer was inserted ... registers, which naturally shifted right. ...
    (comp.programming)
  • Re: A bit off topic; setjmp/longjmp
    ... You need a buffer large enough to hold all registers. ... In setjmp, save the return address in the buffer. ... after restoring all registers (without restoring the ...
    (comp.lang.c)