Search code examples
assemblybufferoffsetx86-16tasm

Assembly (TASM): print the sum of certain bits in bytes


My goal is to print the sum of every byte's 0th and 3rd bit. This is my code so far:

printLine macro line
    mov ah, 09
    mov dx, offset line
    int 21h
endm
;-----------------------------
readLine macro buffer
    mov ah, 0Ah
    mov dx, offset buffer
    int 21h
endm
;-----------------------------
getByteBitSum macro theByte
    mov al, byte ptr theByte
    mov cl, byte ptr theByte
    shr al, 3
    and al, 01
    and cl, 01
    add al, cl
endm
;-----------------------------
;-----------------------------
;-----------------------------
.model small
    ASSUME CS:code, DS:data, SS:stack
;-----------------------------
data segment para public 'DATA'
    message_1:
        db 'Enter a line'

    newLine:
        db 0Dh, 0Ah, '$'

    message_2:
        db 'You entered ',0Dh, 0Ah, '$'

    dataBuffer:
        db 20, 00, 20 dup (00)
data ends
;-----------------------------
code segment para public 'CODE'
    start:

    mov ax, seg data
    mov ds, ax

    printLine message_1

    readLine dataBuffer
    printLine newLine

    printLine message_2
    printLine newLine

    mov bx, 0000
    mov bl, byte ptr[dataBuffer + 1]
    mov word ptr [dataBuffer + bx + 3], 240Ah

    printLine dataBuffer + 2
    printLine newLine

    getByteBitSum [dataBuffer + 2]
    printLine newLine

    getByteBitSum [dataBuffer + 3]
    printLine newLine

    getByteBitSum [dataBuffer + 4]
    printLine newLine

    mov ah, 4ch
    int 21h
code ends
;-----------------------------
stack seg para stack 'STACK'
    dw 400h dup ('**')
stack ends
;-----------------------------
    end start

The error I get is :

GETBYTEBITSUM (1) Need right square bracket
GETBYTEBITSUM (2) Need right square bracket
GETBYTEBITSUM (1) Need right square bracket
GETBYTEBITSUM (2) Need right square bracket

My guess is that I don't really understand how buffer and its offset does work. If my conjecture is right, could anyone explain in short using this example what is happening with it?

BTW: As of right now I am only trying to print the first 3 bytes, not the whole input line.

Thank you.


Solution

  • getByteBitSum [dataBuffer + 2]
    

    The macro expansion has difficulties with the embedded space characters!
    Solve it by writing:

    getByteBitSum [dataBuffer+2]   ;No more embedded spaces!