Skip to content

Instantly share code, notes, and snippets.

@Dascr32
Created September 9, 2015 07:49
Show Gist options
  • Save Dascr32/32ae1cf612538d7183d3 to your computer and use it in GitHub Desktop.
Save Dascr32/32ae1cf612538d7183d3 to your computer and use it in GitHub Desktop.
; Author: Daniel Aguilar S.
; IS: 8086 Assembly.
; Desc: Prints a positive or negative number.
org 100h
mov al, 11010101b ; Number to be displayed. (8 bits)
print proc
mov bx, 0AH ; Initialize divisor by 10
mov cx, 0H ; Clean CX
mov dx, 0H ; Clean DX
.isNegative:
mov dx, ax ; Move number to DX to check if its negative
shr dx, 7H ; Shift 7 bits to the right (leaves the most significant bit)
cmp dx, 1H ; Check if DX equals to 1 (negative)
je .negative ; If DX is equal to 1 add a "-" and apply two's complement to AX
jmp .split ; Otherwise (positive) make normal split and convert
.negative:
push 0C0H ; Push character "-", ASCII code - mask(48) = 192
inc cx ; Increment to track the number of characters
mov ah, 0FFH ; Put high 8 bits of ax in 255 to apply two's complement correctly
neg ax ; Apply two's complement to AX and continue to split and convert
.split:
mov dx, 0H ; Clean DX
div bx ; Divide AX by BX
push dx ; Push DX reminder to stack
inc cx ; Increment to track the number of characters
cmp ax, 0H ; Check if its possible to keep dividing
jne .split ; Loop again if AX is not 0
.convert:
pop dx ; Pop from stack to dx
add dx, 30H ; Apply mask and convert to ASCII
mov ah, 02H
int 21h ; Call OS to print character
loop .convert ; Loop until cx (counter) is zero.
print endp
ret ; Return control to OS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment