The 'LES' 8086 instruction is not working as intended
Asked Answered
U

1

6

I'm a beginner at 8086 assembly. I'm learning from an old early 90s book I found in a thrift store. I thought it might be fun to program like it's 1992.

Anyway I'm done with the book and now i've written a few programs at the command prompt on my old Win95 laptop.

I'm having issues with this one not working as intended after switching over to using the 'les' instruction. However it did work when I set up the ES and DI registers with the appropriate address manually.

;************************************
;   STACK SEGMENT
;************************************

TheStack SEGMENT STACK      ;STACK specifies the stack segment

    db 64 dup (THESTACK)    ;reserves 512 bytes for the stack

TheStack ENDS

;************************************
;   END STACK SEGMENT
;************************************

;************************************
;   DATA SEGMENT
;************************************

Data SEGMENT

BufAddr DD 0b8000000h

Data ENDS

;************************************
;   END DATA SEGMENT
;************************************

;************************************
;   CODE SEGMENT
;************************************

Code SEGMENT

assume CS:Code,DS:Data

MAIN PROC

Start: ;execution begins

    ;setup input for stosw
    les di, DWORD PTR BufAddr
    mov cx,0f4fh  ;cx contains the number of times stosw will loop
    cld

    ;draw smileys
    mov ax,0f01h ;0f is the text attribute for white on black, 01 is the hex code for a smiley
    rep stosw ;write it all to the buffer

   ;EXIT
    mov AH,4CH ;Setup the terminate dos process service 
    mov AL,0 ;ERRORLEVEL takes 0
    int 21H  ;return to dos

MAIN ENDP 

Code ENDS 

;************************************
;   END CODE SEGMENT
;************************************

END Start ;Start is the Main procedure

Okay, so this program is supposed to draw a bunch of smiley ascii characters in the command prompt window, but it's not working.

It does work when I replace the 'LES' line with the following lines of code.

mov bx,0b800h
mov es,bx
xor di,di

Doesn't the 'LES' instruction when used with the BufAddr variable accomplish the same thing as the previous three lines of code?

When I debug the compiled exe (I'm using MASM 6.11 as the compiler) I notice that the ES and DI registers are not being loaded with the correct values.

Unfamiliar answered 19/8, 2016 at 15:44 Comment(6)
What values are being loaded into es and di instead of the correct values?Prochora
Try adding mov ax, SEG data and mov ds, ax statements at the start of your code.Scarlatti
Ross beat me to it. Although you have assume CS:Code,DS:Data in your assembly file, this does not actually set these segment registers. It gives a hint as to what they are suppose to hold. Since the assembler doesn't generate code to set the data segment up and point it at the segment Data you are forced to do it yourself. As Ross suggest mov ax, SEG data mov ds, ax should work. The reason your code doesn't work is because without properly initializing DS at the start of your program the instruction les di, DWORD PTR BufAddr will load BuffAddr from the wrong memory location.Evangelize
les di, DWORD PTR BufAddr is the same as les di, DWORD PTR [DS:BufAddr] the default segment register for the memory load is DS . If DS is wrong then it will fail to load from the proper memory address,Evangelize
Note that db 64 dup (THESTACK) doesn't assemble for me. I'd suggest using db 512 dup (?) instead.Scarlatti
@Ross Ridge Sorry it was a typo. It was supposed to be db 64 dup ("THESTACK")Unfamiliar
P
7

Before loading the segment and offset from RAM, you need to set the DS register to actually point to your data segment. By default, DS points to your PSP, which isn't the place you want it to point to.

Prochora answered 19/8, 2016 at 15:57 Comment(10)
I want ES to hold the value 0b800h, and DI to hold 0. B800:0000 is the address of the DOS refresh buffer.Unfamiliar
@Proughgrammor What values are loaded into ES and DI instead of the correct values? You mention that you saw wrong values being loaded, what were these?Prochora
9FFFh is loaded into ES, and 20CDh is loaded into DI.Unfamiliar
@Proughgrammor Did you make sure that DS is loaded with the right segment when main starts?Prochora
Oops no! Maybe that's the issue.Unfamiliar
@Proughgrammor Tell me if that's the case so I can rewrite my answer.Prochora
Thanks, it works now, but I didn't have to swap the value of the variable around. Maybe the assembler does that on it's own?Unfamiliar
@Proughgrammor It appears that my original suspicion (that you need to swap the values around) was wrong.Prochora
Actually the value in DS points to the PSP, which in the EXE format executable is the wrong place.Scarlatti
DS is a determinate value when the EXE begins running (after the DOS EXE loader is finished). It just doesn't happen to be a value that won't point to the correct location. As Ross points out that value DOS EXE loader stores into DS is the Program Segment Prefix.Evangelize

© 2022 - 2024 — McMap. All rights reserved.