8111

8086 assembly division

I have this problem with the code below which converts number to ASCII 'number-text'. However the code seems to loop at the 'div' opcode

;Main Program main: mov ax, 0x0000 mov ds, ax ; setup data segment register mov si, GreetString ; setup data segment offset call print_string ; call print string procedure mov si, DataWord ; setup data segment offset mov bl, 0x000A ; base 10 call format_string ; call format string procedure mov si, GreetString ; setup data segment offset call print_string ; call print string procedure prevent_overflow: hlt ; halt the CPU jmp prevent_overflow format_string: mov ax, [ds:si] ; load register ax with data xor cx, cx ; set counter to 0 mov si, GreetString ; set pointer to address of GreetString .format_char: div bl ; divides by register bl add ah, 48 ; convert to ascii numeric mov [ds:si], ah ; move ascii numeric to ds:si inc cx ; increase counter inc si ; increase si xor ah, ah ; clear ah register or ax, ax jnz .format_char ; jump to format next char ret

Any idea why does this happen? I say 'loop' because my CPU is at max-value and does not proceed with the opcodes after div

*Updated. Thanks newgre. Added main section to make it more readable (^.^)

Answer1:

The problem is that you get a division overflow exception because the quotient (in my example below it's 12345 div 10 = 1234 + remainder of 5) doesn't fit into the 8 bits of al (remember that ah is already used by the remainder?).

Every time either the quotient or the remainder doesn't fit into the destination register you get a division overflow exception. It's not only the division by 0 that causes it.

You want to do a 32/16=16:16 division instead of a 16/8=8:8 division.

Here's what works in DOS with my changes (marked as ;;;):

; file: div2.asm ; compile as: nasm -f bin div2.asm -o div2.com org 0x100 ;;; ;Main Program main: mov ax, 0x0000 ; mov ds, ax ; setup data segment register ;;; mov si, GreetString ; setup data segment offset call print_string ; call print string procedure mov si, DataWord ; setup data segment offset mov bl, 0x000A ; base 10 call format_string ; call format string procedure mov si, GreetString ; setup data segment offset call print_string ; call print string procedure ret ;;; prevent_overflow: hlt ; halt the CPU jmp prevent_overflow format_string: mov ax, [ds:si] ; load register ax with data xor cx, cx ; set counter to 0 mov si, GreetString ; set pointer to address of GreetString .format_char: ; div bl ; divides by register bl ;;; xor dx, dx ;;; xor bh, bh ;;; div bx ;;; ; add ah, 48 ; convert to ascii numeric ;;; add dl, 48 ;;; ; mov [ds:si], ah ; move ascii numeric to ds:si ;;; mov [ds:si], dl ; move ascii numeric to ds:si inc cx ; increase counter inc si ; increase si ; xor ah, ah ; clear ah register ;;; or ax, ax jnz .format_char ; jump to format next char mov byte [ds:si], "$" ;;; inc cx ; increase counter ;;; inc si ; increase si ;;; ret print_string: ;;; pusha ;;; mov ah, 9 ;;; mov dx, si ;;; int 21h ;;; popa ;;; ret ;;; DataWord dw 12345 ;;; GreetString db "Hello World!", 13, 10, "$" ;;;

It prints:

Hello World! 54321

Recommend

  • Calculated Member for the TOTAL of a measure for multiple Dimensions
  • Change the context with Subselect MDX from Excel client
  • Assign value to struct type constants in C
  • Arduino Sensor 10DOF MPU9255 cannot fetch magnetometer data
  • UdpClient Receives Data on multiple listeners
  • Data transformation during load
  • python file I/O with binary data
  • How send NfcA command to the MIFARE card?
  • How can I calculate Longitudinal Redundancy Check (LRC)?
  • Jump to Protected Mode not working after linking the kernel
  • python struct unpack
  • can you declare data in a char** using hex?
  • Unable to execute Ruby script from file, but IRB works fine?
  • How to build a serial (RS422 or RS232) message to communicate with Sick LMS200 via PuTTy?
  • dot 2 pixels together
  • How to create a binary executable using hex?
  • In x86 assembly, is ESP decremented twice after a call and then push, before data is saved on the st
  • Is there a built in python function to escape a string of unicode characters into unicode escape seq
  • Flexbox: how to add margin / vertical spacing for the case there is more than one row?
  • Rendering  In Front of Special Characters
  • Image scaling geometry
  • Assembly Language Absolute addresses and segment registers
  • Qpython unicode strings
  • Safari WebCrypto API RSA-OAEP encryption
  • x86: Count transitions from 1 to 0 in 32 bit number
  • TensorFlow C++, runtime issue
  • Deployments not visible in Kubernetes Dashboard
  • Which browser have this strange user agent? (IOS device)
  • accepts_nested_attributes_for practical form use for in Rails 3
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Object and struct member access and address offset calculation
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • d3 v4 drag and drop with TypeScript
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • How to load view controller without button in storyboard?