; ==================== ; WriteInt(short VAL) ; ==================== ; Writes an integer to the console. .data WriteIntField db 7 DUP (0) WriteIntBase dw 10 .code WriteInt proc near ;void WriteInt(short VAL) { pusha pushf ; clear out the field string, and put the pointer at the last byte mov bx, offset WriteIntField ; bx = WriteIntField mov cx, 5 ; cx = 5 WriteIntLoop0_start: ; while (cx > 0) { mov [bx], byte ptr ' ' ; *bx = ' ' inc bx ; bx++ loop WriteIntLoop0_start ; dec cx } ; grab the argument mov bp, sp mov ax, [bp+20] ; ax = VAL ; if the argument is negative, invert it cmp [bp+20], word ptr 0 ; if (VAL >= 0) { jge WriteIntIf0_end neg ax ; ax = -ax WriteIntIf0_end: ; } ; repeatedly divide the argument by 10, printing the quotient WriteIntLoop1_start: ; do { mov dx, word ptr 0 ; dx = 0 div WriteIntBase ; ax /= WriteIntBase add dl, '0' ; *bx = (ax % WriteIntBase) + '0' mov [bx], dl dec bx ; bx-- cmp ax, 0 ; } while (ax > 0) jg WriteIntLoop1_start ; if the argument was negative, now insert the - sign cmp [bp+20], word ptr 0 ; if (VAL < 0) { jge WriteIntIf1_end mov [bx], byte ptr '-' ; *bx = '-' WriteIntIf1_end: ; } ; call WriteStr to print the string that was generated push offset WriteIntField ; WriteStr(WriteIntField) call WriteStr popf popa ret 2 ; return WriteInt endp ;} ; ===================== ; WriteStr (char * ptr) ; ===================== ; writes a null-terminated string to the console. WriteStr proc near ;void writestr(char * ptr) { pusha pushf mov bp, sp mov bx, [bp+20] mov ah, 2 WriteStr_loop0_start: test byte ptr [bx], 11111111b jz WriteStr_loop0_end mov dl, [bx] int 21h inc bx jmp WriteStr_loop0_start WriteStr_loop0_end: popf popa ret 2 ; return WriteStr endp ;}