0

So I am trying to make a printf process in assembly 86x (using emu8060) to make it easier to display stuff to the screen, I am using the register bx as a parameter, so I pass a variable inside, and then in1 printf1 process, I am moving the value bx into another variable called ln, then displaying it. the problem is, it just displays llo world

Here is my code:

.model tiny
.code
org 100h


printf proc 
    mov ln, bx ; SHOULD give me the value of var bye (a dw)
    mov ah, 09h
    mov dx, offset ln
    int 21h
    
    mov ah, 4ch
    mov al, 00
    int 21h
    
    ret
    printf endp

main proc near
    mov bx, bye
    call printf
endp
ln dw "Hello world $"  
bye dw "Bye WORLD $"
end main

; output: llo world  
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
theLawyer
  • 13
  • 5
  • 1
    2 things: don't overwrite the first 2 bytes of the the string by storing `bx` into it. (I'd have expected `Byllo world ` as the output, since `main` loaded 2 bytes from the string, instead of passing a pointer.) Also, in some MASM-syntax assemblers, `dw` and `dd` are weird for strings: [When using the MOV mnemonic to load/copy a string to a memory register in MASM, are the characters stored in reverse order?](https://stackoverflow.com/q/57427904) – Peter Cordes Jun 06 '22 at 19:41
  • 1
    Also, a normal `printf` function doesn't `exit` your process. The AH=4Ch DOS call should be in `main`. – Peter Cordes Jun 06 '22 at 19:42
  • @PeterCordes so if i dont use bx, what should i use – theLawyer Jun 06 '22 at 19:50
  • 2
    You should have the caller pass a pointer, and pass it to int 21h / AH=09. Then you'll have a `fputs` implementation for `$`~terminated strings. IDK what exactly you were hoping this code would do, or why you'd want it to print the string from `ln` when the caller passed something different. – Peter Cordes Jun 06 '22 at 20:03

0 Answers0