SoFunction
Updated on 2025-03-04

Assembler, the implementation of table-seeking and squared

[Task] Set the memory unit SQTAB first address to store a square number table of 0 to 15. It is required to use the direct table lookup method to calculate a program, and find the square value of the given number (<=15) in unit A and send it to unit B for storage.

【Reference Answer 1】

assume cs: code, ds: data

data segment
  sqtab db 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
     db 100, 121, 144, 169, 196, 225
  a db 12
  b db ?
data ends

code segment
start:
  mov ax, data
  mov ds, ax

  mov bl, a      ;byaMedian value as index value
  mov bh, 0
  mov al, sqtab[bx]  ;existtableFind in
  mov b, al

  mov ax, 4c00h
  int 21h

code ends
  end start

[Reference Answer 2] (Use special table lookup instructions xlat)

assume cs: code, ds: data

data segment
  sqtab db 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
     db 100, 121, 144, 169, 196, 225
  a db 12
  b db ?
data ends

code segment
start:
  mov ax, data
  mov ds, ax
  mov bx, offset sqtab

  mov al, a
  xlat
  mov b, al

  mov ax, 4c00h
  int 21h

code ends
  end start

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.