1

I'm trying to display characters on screen by calling the following function in my C code:

.global _putInMemory

;void putInMemory (int segment, int address, char character)
    _putInMemory:
        mov bp,sp
        push ds
        mov ax,[bp+2]
        mov si,[bp+4]
        mov cl,[bp+6]
        mov ds,ax
        mov [si],cl
        pop ds
        ret

like this:

    int segment_start = 0xB000;
    putInMemory(segment_start, 0x8140, 'R');
    putInMemory(segment_start, 0x8141, 0x1F);
    putInMemory(segment_start, 0x8142, 'A');
    putInMemory(segment_start, 0x8143, 0x1F);

However, this only displays the first letter without the color, but when I specify this in the code like this, it works just fine:

putInMemory(0xB000, 0x8140, 'R');
putInMemory(0xB000, 0x8141, 0x1F);
putInMemory(0xB000, 0x8142, 'A');
putInMemory(0xB000, 0x8143, 0x1F);

I don't understand why it does not work if I store int in a variable. Is int too small? I'm using the bcc compiler

Edit: The correct code would be this:

;void putInMemory (int segment, int address, char character)
_putInMemory:
    push bp
    mov bp,sp
    push ds
    mov ax,[bp+4]
    mov si,[bp+6]
    mov cl,[bp+8]
    mov ds,ax
    mov [si],cl
    pop ds
    pop bp
    ret
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93

1 Answers1

1

Make sure you know and follow the calling convention bcc uses. Typical compilers will expect the bp and si registers (among others) to be preserved across function calls. The compiler may be using bp to address your segment_start local variable, but you destroy bp so on the second function call some garbage will be passed instead, producing the observed behavior.

Try saving and restoring bp and si (just like you do with ds) to see if it fixes the problem.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • i tried pushing `bp`, `si` and `ds` to the stack and retrieving them by poping them off at the end of the function in reverse order, but this did not seem to fix it – Uku Loskit Jun 11 '13 at 06:43
  • Try saving all registers then (conveniently using `pusha`/`popa`) - the compiler may be using any of the caller-saved registers to hold the `segment_start` variable. – Jester Jun 11 '13 at 10:29