News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Displaying register values

Started by zak100, December 16, 2009, 01:50:25 PM

Previous topic - Next topic

zak100

Hi,
I am trying to display register values. I got this code from internet. Instead of runing the entire code, I have commented some portion of it and I am trying to print register names using SI.


.MODEL  TINY
        .CODE

;----------------------------------------------------------------------------------

LoadOfs EQU     0               ;must match the value in the bootloader source file

;----------------------------------------------------------------------------------

;---------------------- initialize ES segment register

        ORG     0

Start:  push    cs
        pop     ds
       

    push sp   ;
    push ss   ;
    push es   ;
    push ds   ;
    push cs   ;
    push bp
    push si
    push di
    push dx
    push cx
    push bx
    push ax
;-----------clear screen
mov ax, 3
int 10h

   
;---------------------- writing a message on screen at startup, character by character- we can't use int 21h
overdata:
    xor di, di
    mov ax, 0B800h
    mov es, ax
    mov di, 80 * 5 * 2
    mov cx, 13       ;
    mov si, offset regnames
    cld
nextreg:
    push cx          ; save reg-loop counter
    mov cx, 5        ; char counter for names
    mov ah, 3        ; color
nextchar:
    lodsb            ;loads al with data pted by si
    stosw            ;stores al at mem pted by es:di
    loop nextchar
    pop cx           ; reg-loop counter

    ;pop dx           ; value of reg
    ;mov bx, 4        ; nibble loop counter
;nextnibble:
;   rol dx, 4      ; rotate a nibble into position
  ;  mov al, dl     ; make a copy to process
   ; mov ah, 03h    ; color
   ; and al, 0Fh    ; isolate nibble

;    cmp al, 10     ; short way to convert a nibble
;   sbb al, 69h    ; to hex ascii
  ;  das

;    stosw          ; write char and attribute to screen
;   dec bx         ; loop counter for nibbles
  ;  jnz nextnibble
    add di, 80 * 2 - 18     ; next line
    loop nextreg
       

;---------------------- done - halt
mov ah, 4ch
int 21h
Halt0: hlt
jmp     Halt0

;---------------------- data area in code segment

Msg0    db      "We be bootin234!",0
regnames     db 'ax = '
    db 'bx = '
    db 'cx = '
    db 'dx = '
    db 'di = '
    db 'si = '
    db 'bp = '
    db 'cs = '
    db 'ds = '
    db 'es = '
    db 'ss = '
    db 'sp = '
    db 'ip = '

;----------------------------------------------------------------------------------

        END     Start





Right now its printing garbage. Can somebody plz help me with this?

Zulfi.

zak100

I have used cld for incrementing si but there are two loops and I am confused with the scope of cld.

Zulfi.

dedndave

Zulfi !!! -this code won't work - lol

mov ah, 4ch
int 21h

i suggest something like this
fill in the values with hexidecimal characters, then display them all at once
you could even show the flags a la debug   :bg
BUT - i would write the program in an EXE or COM form to work the bugs out - then put it into the boot code

regnames db 'ax = '
r_ax    db 4 dup(?),20h
        db 'bx = '
r_bx    db 4 dup(?),20h
        db 'cx = '
r_cx    db 4 dup(?),20h
        db 'dx = '
r_dx    db 4 dup(?),20h
        db 'di = '
r_di    db 4 dup(?),20h
        db 'si = '
r_si    db 4 dup(?),20h
        db 'bp = '
r_bp    db 4 dup(?),0Dh,0Ah
        db 'cs = '
r_cs    db 4 dup(?),20h
        db 'ds = '
r_ds    db 4 dup(?),20h
        db 'es = '
r_es    db 4 dup(?),20h
        db 'ss = '
r_ss    db 4 dup(?),20h
        db 'sp = '
r_sp    db 4 dup(?),20h
        db 'ip = '
r_ip    db 4 dup(?),0Dh,0Ah,0

dedndave



as for the scope of CLD - it stays cleared until you set it - you should only need that once

dedndave

code for converting 4 bits to ASCII hex
an oldie but a goodie.......

        ADD     AL,90h
        DAA
        ADC     AL,40h
        DAA

AL register:

hex    hex      ascii
in       out      out
0        30       0
1        31       1
2        32       2
3        33       3
4        34       4
5        35       5
6        36       6
7        37       7
8        38       8
9        39       9
A        41       A
B        42       B
C        43       C
D        44       D
E        45       E
F        46       F

zak100

Hi,
Thanks for your reply.

Kindly tell me why are using 4 bytes? these registers are 16 bit.

I am confused with cld because there are two loops. First print the characters with in a string (size=5) and then there are 13 strings for registers.After finishing with 1st string using CX=5 in the inner loop, will it go to the next string for the next register directly?? Sorry If cant make you understand.

Zulfi.

FORTRANS

Hi,

QuoteKindly tell me why are using 4 bytes? these registers are 16 bit.

   16 bits is four hexadecimal digits.  Thus uses four bytes
to display.

    Wether you use the DAA algorithm that Dave posted, the
DAS algorithm in your code, or a test and jump as in the code
I posted in the other thread to convert binary to hexadecimal,
you should think about making it a subroutine.  See the TOASCII
and CONVERT routines in that program for ideas.

   The best way to see how the string instructions work is
probably to look at them by stepping through your code in debug.

Steve N.

zak100

Thanks for your reply. Right now I am interested in finding out why the strings are not being printed. Thats why I have commented the rest of the code. I would try to find the error through Debug. I would let you people know about the progress in this regard.


Zulfi.

zak100

Hi,
I have checked using debug and I found that lodsb is not working properly. After lodsb AL must get the first character of 1st string pted (arrayed) by regnames but it doesnt work. The prob might be due to following instruction:


mov si, offset regnames


Kindly somebody plz help me in this regard.

Zulfi.

Neil


MichaelW

One possibility is that the code is not being loaded into memory starting at offset 0. This would not cause problems with the code labels, but it would cause problems with the data labels.
eschew obfuscation

dedndave

here you go Zulfi
i wanted this routine anyways - it is useful for other things

AX=0000  BX=0000  CX=00FF  DX=055B  DS=055B  SI=0000  ES=055B  DI=0200
CS=056B  IP=0000  BP=091E  SS=0588  SP=0200 NV UP EI NT PL NZ NA PO NC

Press Any Key to Exit


EDIT - Michael is right, too

        mov     si,offset regnames+LoadOfs

you should add the LoadOfs value to all data references
that way, if you change the load address in the boot sector, you only have to update the Loadfs EQUate
once you get local, you can get rid of that by re-setting the segment registers

zak100

Hi,
Thanks for your reply. I would try what you have suggested but LoadOfs is equal to zero. Whay is it so important. In the previous just for the sake of checking, I ran the program without it but it worked. As you advised I am right excuting it as an .exe file and not as a kernel associated with the boot sector.

Zulfi.

zak100

Hi,
I have not tried your code in the zip file. I am trying to find out error in this program. I have added LoadOfs in the 'mov' instruction but its not yet working.

Zulfi.

dedndave

i haven't tested this, Zulfi, but give it a try   :P

        .MODEL  TINY
        .CODE

;--------------------------------------------------------------------------------------

LoadOfs EQU     0                   ;must match the value in the bootloader source file

;--------------------------------------------------------------------------------------

        ORG     0

Start:  call    Push_IP             ;this CALL pushes the IP register

Push_IP:
        push    sp
        push    ss
        push    es
        push    ds
        push    cs
        push    bp
        push    si
        push    di
        push    dx
        push    cx
        push    bx
        push    ax

        mov     ax,3
        int     10h

        mov     ax,cs
        add     ax,LoadOfs/16
        mov     ds,ax

        mov     ax,0B800h
        mov     es,ax
        cld

        mov     bp,sp               ;use BP to access the stack values
        sub word ptr [bp+24],3      ;adjust for 3 byte CALL
        add word ptr [bp+22],2      ;adjust for the CALL

        mov     si,offset RegText
        mov     di,5*2*80
        mov     ah,3                ;attribute
        mov     cx,13               ;13 registers

nextreg:
        push    cx                  ;save reg-loop counter
        mov     cx,5                ;char counter for names

nextchar:
        lodsb                       ;loads AL from ds:[si]
        stosw                       ;stores AX at es:[di]
        loop    nextchar

        mov     dx,[bp]             ;get the stack value
        call    B2Hex
        add     bp,2
        pop     cx                  ;reg-loop counter
        add     di,2*(80-9)         ;next line
        loop    nextreg

        mov     sp,bp               ;discard the stack values

Halt0:  hlt
        jmp     Halt0

;--------------------------------------------------------------------------------------

HexHi   PROC    NEAR

        mov     ah,0
        shr     ax,1
        shr     ax,1
        shr     ax,1
        shr     ax,1

HexLo:: and     al,0Fh
        add     al,90h
        daa
        adc     al,40h
        daa
        mov     ah,bh
        stosw
        ret

HexHi   ENDP

;--------------------------------------------------------------------------------------

B2Hex   PROC    NEAR

;   AH = attribute
;   DX = value to display as hexadecimal
;ES:DI = video buffer address

        mov     bh,ah               ;BH = attribute
        mov     al,dh
        call    HexHi
        mov     al,dh
        call    HexLo
        mov     al,dl
        call    HexHi
        mov     al,dl
        call    HexLo
        ret

B2Hex   ENDP

;--------------------------------------------------------------------------------------

RegText db 'ax = '
        db 'bx = '
        db 'cx = '
        db 'dx = '
        db 'di = '
        db 'si = '
        db 'bp = '
        db 'cs = '
        db 'ds = '
        db 'es = '
        db 'ss = '
        db 'sp = '
        db 'ip = '

;--------------------------------------------------------------------------------------

        END     Start