| Electronics > Beginners |
| 8086 Assembly code question. |
| (1/2) > >> |
| ammjy:
Hello, EEVBlog peoples. always thanks for your help. Now, I trying understand one assembly code of 8086 that do judge positive or negative about number. but, example code doesn't work. always code give 045 value to result. i already tried 4 hours but, couldn't solve. not yet didn't find which line is a problem. if you wouldn't mind, i want request help. please, let me know... something i missed ? i use emu8086 compiler. ----------------------------------------------------------- data segment x db '-120' ; number for judge. -123 ~ +127 data ends stack segment dw 100 dup(0) stack ends code segment assume cs:code, ss:stack, ds:data main proc mov ax, data mov ds, ax test x, 80h jz positive mov dl, '-' mov ah, 2 int 21h neg x positive: mov al, x mov ah, 0 mov bl, 100 div bl ; ax / bl = ah mov bh, ah mov dl, al add dl, 30h mov ah, 2 int 21h ; hundred mov al, bh ; remainder mov ah, 0 mov bl, 10 div bl ; ax / bl mov bh, ah mov dl, al add dl, 30h mov ah, 2 int 21h mov dl, bh add dl, 30h mov ah, 2 int 21h ; unit mov ah, 4ch int 21h main endp code ends end main ----------------------------------------------------------- |
| 0xdeadbeef:
To see if an integer number is signed or not, you just need to check the uppermost bit. This should also be possible with "cmp ax,0" followed by "jl <label>" (jump if lower) or "jge <label>" (jump if greater or equal) - assuming you consider 0 as positive. Side note: there are lots of DOS int21 calls to print characters and the like. Obviously they only work under DOS. |
| Paul Rose:
I don't know about emu8086, but ususally with the db directive, if you put the constant in quotes, it is interpreted as an ASCII string ( x is 4 bytes: a ASCII minus sign, an ascii '1', ascii '2', ascii '0') If you want a byte with -120, just say: x db -120 |
| Paul Rose:
Followup. The problem is indeed with the db and quotes. The first character of '-120' is '-' The ascii value for '-' is 45 The "test x, 80h" is testing the high bit of the byte at x (which is 45) to see if the byte is negative. 45 is positive. You then enter the routine that divides by 10 three times to generate a three digit decimal number '045' |
| Zero999:
It's years since I've done this. Why not use the cmp instruction to see whether the first character is '-'? Is the string always four characters long? If not, how does the program know? If the sting might be shorter than four characters, the usual way of determining the length is to use a zero value i.e. ASCII null to indicate the end of the string. Another way it to set the first byte or word to the length of the string. Each method has its own pitfalls. The ASCII null will cause problems if the null terminator is missing and the byte/word header limits the maximum possible string length. How are you going to deal with invalid characters in the string? |
| Navigation |
| Message Index |
| Next page |