8086 Assembly code question. - Page 1
Forum section: electronics / beginners

author: ammjy
author_rank: Regular Contributor
timestamp: December 20, 2018, 03:54:59 pm
content: 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 segmentx db '-120' ; number for judge. -123 ~ +127data endsstack segmentdw 100 dup(0)stack endscode segmentassume cs:code, ss:stack, ds:datamain procmov ax, datamov ds, axtest x, 80hjz positivemov dl, '-'mov ah, 2int 21hneg xpositive: mov al, xmov ah, 0mov bl, 100div bl ; ax / bl = ahmov bh, ahmov dl, aladd dl, 30hmov ah, 2int 21h ; hundredmov al, bh ; remaindermov ah, 0mov bl, 10div bl ; ax / blmov bh, ahmov dl, aladd dl, 30hmov ah, 2int 21hmov dl, bhadd dl, 30hmov ah, 2int 21h ; unitmov ah, 4chint 21hmain endpcode endsend main-----------------------------------------------------------

author: 0xdeadbeef
author_rank: Super Contributor
timestamp: December 20, 2018, 04:05:10 pm
content: 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.

author: Paul Rose
author_rank: Regular Contributor
timestamp: December 20, 2018, 06:19:54 pm
content: 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

author: Paul Rose
author_rank: Regular Contributor
timestamp: December 20, 2018, 09:04:11 pm
content: Followup. The problem is indeed with the db and quotes.The first character of '-120' is '-'The ascii value for '-' is 45The "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'

author: Zero999
author_rank: Super Contributor
timestamp: December 20, 2018, 09:15:37 pm
content: 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?

author: Paul Rose
author_rank: Regular Contributor
timestamp: December 21, 2018, 04:15:24 am
content: I downloaded emu8086 and ran it (windows app, but it ran fine under Wine on linux).I pasted your code into a new "EXE template" and compiled, saved, and ran it.I got the same045result that you got.I then replaced the 2nd line:x db '-120' ; number for judge. -123 ~ +127withx db -120 ; number for judge. -123 ~ +127(just removed the single quotes around the -120)The output was-120which I believe is what you expected.Changing the 2nd line tox db 127output127which is also correctEverything else about the program is correct.The error was that the 2nd line you posted:x db '-120'set up 4 bytes starting at x:45, 49, 50, 48which are the ASCII values of the characters in the string '-120'You have to keep the assemble (compile) time separate in you mind from run time. The assembler (compiler) understands ASCII strings, numbers in decimal, and numbers in hex, and can store these values as constants in the .EXE file. The CPU only understands how to do math and bit operations on bytes (and also "words" made up of 2 bytes). The CPU itself has no concept of ASCII. The operating system ( which you called into with the "int 21h" instructions) understands how to print ASCII characters to the console.